Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于预呈现/预回迁空白页的Javascript计时器_Javascript_Html_Timer_Prerender - Fatal编程技术网

用于预呈现/预回迁空白页的Javascript计时器

用于预呈现/预回迁空白页的Javascript计时器,javascript,html,timer,prerender,Javascript,Html,Timer,Prerender,JS计时器在页面上的所有内容用完时将清空 window.setTimeout(function() { document.write("<link rel='prerender' href='http://google.com' />") } ,10000 ); window.setTimeout(函数(){ 文件。写(“”) } ,10000 ); 当JS放置在头部和身体内时,效果都会发生 如何基于计时器进行预取/预呈现,而不使页面变为空白?(所有内容消失。没有url重

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

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


}
,10000
); 
window.setTimeout(函数(){
文件。写(“”)
}
,10000
); 
当JS放置在头部和身体内时,效果都会发生


如何基于计时器进行预取/预呈现,而不使页面变为空白?(所有内容消失。没有url重定向。我只剩下一个空白页)

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

如果您使用的是jQuery,则可以使用其函数:

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'));

根据您的回答类似的问题,我使用jQuery,然后使用$(“head”).append('