简体   繁体   English

Javascript,在重定向后等待页面加载然后执行代码

[英]Javascript, waiting for page to load after a redirect then executing code

Hi I've seen this solution posted several times on this site and the answer usually is one of the following. 嗨,我已经看到此解决方案在此站点上发布了好几次,答案通常是以下之一。

$( document ).ready()
window.onload="myFunction()";

I'm making a google extension that redirects to a new page using chrome's tab update then using chrome's tab executescript to introduce a new script to that newly redirected page to click a button. 我正在做一个google扩展程序,它使用chrome的选项卡更新重定向到新页面,然后使用chrome的选项卡executescript将新脚本引入该新重定向的页面以单击按钮。 I would redirect the page then using the above methods I tried to get the new script to run on the redirected page instead of the current page. 我将重定向页面,然后使用上面的方法尝试使新脚本在重定向页面而不是当前页面上运行。 Sadly none of them worked, they would still let the new script to be put in on the old page. 遗憾的是,它们都不起作用,他们仍然允许将新脚本放入旧页面。 So to get around this I just used setTimeout and set it to 1500, roughly enough time for me to load the new page, but that isn't very efficient and it might be different for users is there something I'm doing wrong or is this the only solution? 因此,为了解决这个问题,我只使用了setTimeout并将其设置为1500,这大约足以让我加载新页面,但这并不是非常有效,并且对于用户来说可能有所不同是因为我做错了还是这是唯一的解决方案?

My code looks something like this. 我的代码看起来像这样。

chrome.tabs.update({url: "www.example.com"});
pause(); // I just took the liberty of shorting the whole setTimeout part
chrome.tabs.executeScript(null, {file: "jquery.js"}), console.log("jquery loaded");

When I tried using the original solutions at the beginning of the page I'd just wrap the execute script in it to make it look something like this. 当我尝试在页面开头使用原始解决方案时,我只是将执行脚本包装在其中,以使其看起来像这样。

chrome.tabs.update({url: "www.example.com"});
pause(); // I just took the liberty of shorting the whole setTimeout part
$( document ).ready(){
  chrome.tabs.executeScript(null, {file: "jquery.js"}), console.log("jquery loaded");
}

What I'm currently doing is using a short setTimeout of like 250 and then wrapping my code in .ready() which /works/ but I'd much rather remove setTimeout all together which is the point of this question. 我目前正在做的是使用短的setTimeout,例如250,然后将代码包装在.ready()/ works /中,但我宁愿将setTimeout一起删除,这就是这个问题的重点。 Thank you in advance! 先感谢您! If you need more information just ask. 如果您需要更多信息,请询问。

I know nothing of chrome.tabs, etc but using a timer is a hack. 我对chrome.tabs等一无所知,但是使用计时器是很容易的事。 Any async invocation needs a callback, ie a function that triggers when the calling function has finished doing its 'thang. 任何异步调用都需要回调,即在调用函数完成其“ thang”操作时触发的函数。

A quick google and I found this: 一个快速的谷歌,我发现了这一点:

chrome.tabs.update(integer tabId, object updateProperties, function callback)

So you can pass a callback as the last parameter. 因此,您可以将回调作为最后一个参数。 Here's the link , scroll down to [update] section. 这是链接 ,向下滚动到[update]部分。

If you need help with callbacks, post another question and we'll go from there... 如果您需要有关回调的帮助,请发布另一个问题,我们将从那里开始...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM