1

JS 计时器用完时会将页面上的所有内容清空。

window.setTimeout(function() {
document.write("<link rel='prerender' href='http://google.com' />")


}
,10000
); 

当 JS 放置在头部和身体中时,效果都会发生。

如何在不让页面变为空白的情况下基于计时器进行预取/预渲染?(所有内容都消失了。没有 url 重定向。我只剩下一个空白页)

4

1 回答 1

0

document.write 只能在文档准备好显示之前使用。它应该在构建时内联使用。如果在文件准备好后使用它会导致你面临的问题。

如果你使用 jQuery,你可以使用他们的getScript函数:

window.setTimeout(function() {

    $.getScript('script.js',function(){
       //this is your callback function to execute after the script loads
    });
}
,10000
); 

或者,如果仅使用 javascript ,这个问题可能对您有用。他们使用:

(function(d, t) {
var g = d.createElement(t), // create a script tag
    s = d.getElementsByTagName(t)[0]; // find the first script tag in the document
g.src = 'your-script.js'; // set the source of the script to your script
s.parentNode.insertBefore(g, s); // append the script to the DOM
}(document, 'script'));
于 2014-04-02T10:33:27.570 回答