如何等待,直到用javascript加载网页? [英] How to wait until a web page is loaded with javascript?

查看:118
本文介绍了如何等待,直到用javascript加载网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用html改变html中的两个页面,但是当我改变 window.location 时,这个句子后面的代码继续执行。



因此,当我这样做时,例如,调用 getElementById()时,它无法识别该元素,因为该页面是

  function myFun(){

// ...

window.location ='page.html';

// ...等到page.html被加载

}

如何等待页面加载以避免此问题?

解决方案

  window.location ='page.html'; 

您在浏览器中替换页面,其中包含代码 myFun ,通过一个新页面。



如果你想执行它,并且只有在改变页面之后(但我没有看到点),那么你可能会做$ /

pre $ document.onload = function(){window.location ='page.html'; };


I want to change between two pages in html with javascript, but when I change with window.location, the code that is after this sentence continues executing.

So when I do, for example, a call to getElementById() it doesn't recognize the element because the page is still loading.

function myFun(){

    // ...

    window.location = 'page.html';

    // ... wait until page.html is loaded

}

How can I wait until the page is loaded to avoid this problem?

解决方案

When you do

window.location = 'page.html';

you replace the page in the browser, the one containing the code of myFun, by a new page. There is no way for the code following this instruction to be executed.

If you want to execute it, and only after that change page (but I don't see the point), then you might do

document.onload = function(){ window.location = 'page.html'; };

这篇关于如何等待,直到用javascript加载网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆