有没有办法在更改window.location.href后进行onload回调? [英] Is there a way to have an onload callback after changing window.location.href?

查看:906
本文介绍了有没有办法在更改window.location.href后进行onload回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想要做的就是这样做:

Essentially what I'd like to do is something to the effect of this:

window.location.href = "some_location";
window.onload = function() {
  alert("I'm the new location and I'm loaded!");
};

在加载窗口的新位置时,有没有办法进行回调? (上面的代码不起作用。)

Is there any way to have a callback when the window's new location is loaded? (The above code doesn't work.)

推荐答案

不,你不能按照你想要的方式去做。加载新页面将关闭当前文档并开始加载新文档。当新页面开始加载时,当前文档中的任何代码都将不再处于活动状态。

No, you cannot do it the way you want. Loading a new page closes the current document and starts loading a new document. Any code in your current document will no longer be active when the new page starts to load.

要在新文档加载时拥有事件处理程序,您需要将代码插入新页面的文档或将新文档加载到iframe中并监视当前文档中iframe的加载。

To have an event handler when the new document loads, you would either need to insert code into the new page's document or load the new document into an iframe and monitor the loading of the iframe from your current document.

这篇关于有没有办法在更改window.location.href后进行onload回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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