如何等待web页面加载javascript?

时间:2022-08-23 18:00:18

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.

我想在html中使用javascript在两个页面之间进行更改,但是当我使用窗口进行更改时。位置,句子后面的代码继续执行。

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

例如,当我调用getElementById()时,它无法识别元素,因为页面仍在加载。

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?

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

3 个解决方案

#1


11  

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.

您将浏览器中的包含myFun代码的页面替换为一个新页面。执行此指令后的代码没有办法执行。

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

#2


2  

You can use jQuery document.ready or you can create custom bind like this one. In case you use jQuery.

可以使用jQuery文档。准备好了,或者可以像这样创建自定义绑定。如果您使用jQuery。

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

#3


1  

You can't execute code in your own page after doing window.location = 'page.html';. Your page will be replaced by the new page and the code in the current page will no longer be there.

在执行窗口之后,您不能在自己的页面中执行代码。位置= ' page.html ';。您的页面将被新页面替换,当前页面中的代码将不再存在。

The only ways to execute some code after page.html is loaded into the current window are as follows:

执行一页又一页的代码的唯一方法。将html加载到当前窗口如下:

  1. Load it in an iframe or another window and monitor for when the iframe or window has finished loading. In this way, your current code stays around and is not replaced.
  2. 在iframe或其他窗口中加载它,并监视iframe或窗口何时完成加载。这样,您的当前代码就不会被替换。
  3. Have some code in page.html that monitors when it finishes loading and trigger your desired action from there.
  4. 在页面中有一些代码。html,监视何时完成加载并从那里触发所需的操作。

#1


11  

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.

您将浏览器中的包含myFun代码的页面替换为一个新页面。执行此指令后的代码没有办法执行。

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

#2


2  

You can use jQuery document.ready or you can create custom bind like this one. In case you use jQuery.

可以使用jQuery文档。准备好了,或者可以像这样创建自定义绑定。如果您使用jQuery。

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

#3


1  

You can't execute code in your own page after doing window.location = 'page.html';. Your page will be replaced by the new page and the code in the current page will no longer be there.

在执行窗口之后,您不能在自己的页面中执行代码。位置= ' page.html ';。您的页面将被新页面替换,当前页面中的代码将不再存在。

The only ways to execute some code after page.html is loaded into the current window are as follows:

执行一页又一页的代码的唯一方法。将html加载到当前窗口如下:

  1. Load it in an iframe or another window and monitor for when the iframe or window has finished loading. In this way, your current code stays around and is not replaced.
  2. 在iframe或其他窗口中加载它,并监视iframe或窗口何时完成加载。这样,您的当前代码就不会被替换。
  3. Have some code in page.html that monitors when it finishes loading and trigger your desired action from there.
  4. 在页面中有一些代码。html,监视何时完成加载并从那里触发所需的操作。