纯粹的客户端 JavaScript 或 AJAX 解决方案可能比使用元刷新方法更适合您。
如果 URL 只是一个硬编码列表,那么您的解决方案可以完全在 JavaScript 端完成;无需 PHP(提供原始 HTML、CSS 和 JS 除外)。
这是一个解决方案,它遍历 URL 列表并在设定的时间间隔后更新 iframe:http://jsbin.com/lezuquma/1/edit?html,js,output
HTML:
<iframe id="fr" src="http://en.wikipedia.org/wiki/Main_Page"
style="border: 0; width:100%; height:100%;" sandbox="allow-same-origin allow-scripts allow-popups allow-forms"></iframe>
CSS:
html, body {
height: 100%;
width: 100%;
}
JavaScript(注意:使用jQuery):
$(document).ready(function() {
var urls = [
"http://ImportOutpost.com",
"http://blog.appannie.com/",
"http://health.ucsd.edu/specialties/mindfulness/schedule/Pages/default.aspx",
"http://www.treysmithblog.com/",
"http://en.wikipedia.org/wiki/Matrix_(mathematics)",
"http://www.cafepress.com/cp/info/sell/index.aspx?area=openashop&page=openashop",
"http://www.fiverr.com/categories/writing-translation/",
"http://mashable.com/mobile/"
];
var frame = $('#fr');
setInterval(function() {
var currentUrl = urls.shift();
urls.push(currentUrl);
frame.attr('src', currentUrl);
}, 10000);
});
如果由于某种原因无法对 URL 进行硬编码,则只需使用 AJAX 将 URL 从服务器检索到客户端,并使用我上面使用的相同方法来刷新 iframe。
我只让我的解决方案运行几分钟,所以如果你让它连续运行几个小时,它可能仍然会崩溃。根据您选择的刷新间隔,这将是一个密集的操作。