2012-02-08 52 views
4

我也做了以下內容:動態插入到樣式IFRAME

<iframe id="portal" src="page-on-my-domain.php"></iframe> 

$(document).ready(function() { 

    $('#portal').load(function(){ 
     $('#portal').contents().find('head').append('<link href="/css/iframe.css" rel="stylesheet" type="text/css" />'); 
    }); 

}); 

這是工作正常,但問題是,每當我更新的樣式表,不被在IFRAME應用更新的款式。我嘗試重新加載頁面和iframe,但它仍然在拾取舊的樣式。

+0

什麼iframe?您必須向我們展示更多您的標記。 – 2012-02-08 11:14:14

+0

是否調用了'load'處理程序? (檢查,在裏面放一個警報)。 – RoToRa 2012-02-08 12:49:37

回答

4

也可能是瀏覽器緩存?在URL上添加時間戳可能有所幫助:

$(document).ready(function() { 
    $('#portal').load(function(){ 
    var timestamp = +(new Date()); 
    $('#portal').contents().find('head').append('<link href="/css/iframe.css?'+ timestamp +'" rel="stylesheet" type="text/css" />'); 
    }); 
}); 
0

嘗試給css加上時間戳href="style.css?<?php echo date('ljSFYhis'); ?>"這樣做會阻止樣式表的緩存。

但是,我建議這僅用於開發/測試,否則你會每次在每一頁上請求一個新的CSS文件,這會帶來更多的服務器負載。

編輯:所推薦的http://www.electrictoolbox.com/unix-timestamp-javascript/在javascript var ts = Math.round((new Date()).getTime()/1000);時間戳,所以你可以寫爲:

var ts = Math.round((new Date()).getTime()/1000); 
href="style.css?'+ts+'"