jku*_*zak 9 javascript iframe refresh
我在Stack Overflow上找到了很多关于如何用JavaScript刷新iframe的答案.
例如:
他们工作正常.但是,如果最近iframe中的页面已更改,则刷新将不会显示此更改.有没有办法可以强制刷新指定的iframe,以便显示新版本?
use*_*118 10
如果iframe是同源,则可以强制使用完整页面重新加载
iframe.contentWindow.location.reload(true);//The argument "true" will force all loaded resources, such as images, to be re-checked for expiry at the server
Run Code Online (Sandbox Code Playgroud)
iframe,您还可以指示浏览器不要首先缓存它.if(iframe.src.indexOf('timestamp') > -1){ // Check if we still have a timestamp in the URL from a previous refresh
iframe.src = iframe.src.replace(/timestamp=[^&]+/, 'timestamp=' + Date.now()); // And if so, replace it instead of appending so the URL doesn't grow too long.
}else{ // Else we will append the timestamp
iframe.src += (iframe.src.indexOf('?') > -1 ? "&" : "?") + 'timestamp=' + Date.now();// If the URL contains a ?, append ×tamp=...; otherwise, append ?timestamp=...
}
Run Code Online (Sandbox Code Playgroud)
使用new Date().getTime()而不是Date.now()如果对旧的浏览器的支持是很重要的.
使用URL重新加载iFrame,但是给它一个唯一的ID ...
var rand = Math.floor((Math.random()*1000000)+1);
var iframe = document.getElementById('youriframe');
iframe.src = "http://www.yourpage.com/yourpage.html?uid="+rand;
Run Code Online (Sandbox Code Playgroud)
如果我们能够看到您当前拥有的代码,那么为您提供解决方案会更容易.
希望它有所帮助.