0

window.onload似乎没有在 chrome 中运行指定的函数,console我似乎找不到任何有同样问题的人。

代码:

function preStart() {
    console.log("Hello");
}
window.location = 'https://www.google.com/';
window.onload = preStart;

运行时window.location成功运行但"preStart"没有运行。对此有任何帮助,我将不胜感激。

编辑 - Ben Hanna 说.onload不会激活,因为页面已更改,这很好,但有解决方案吗?(页面更改后函数运行的地方)

4

1 回答 1

0

onload如果您更改location为导航到另一个页面,此代码将永远不会触发事件。

更新:你可以做这样的事情。

function preStart() {
   // You can run a function like this before navigating
   console.log("Hello");
   window.location = 'https://www.google.com/';
   // You can't run a function once you've navigated to Google
   // because you can't execute arbitrary scripts on pages/domains
   // that you don't own.
}

// preStart will be executed when the `onload` event fires
window.onload = preStart;
于 2018-05-06T16:05:08.707 回答