gpt4 book ai didi

javascript - inapp 浏览器 window.open 不会触发 loadstart 或 loadstop 事件

转载 作者:行者123 更新时间:2023-11-29 21:20:59 26 4
gpt4 key购买 nike

我有非常简单的代码,但它不会触发任何加载事件 - 窗口打开页面加载但不会触发事件。

var ref = window.open('http://localhost:3000/#/login','_blank','location=no'); 

ref.addEventListener('loadstart', function(event) {
log.info('in loadstart ');
});

ref.addEventListener('loadstop', function(event){
log.info('in loadstop ');
});

https://plnkr.co/edit/ubEk8UN6SGXkYV7PgFZZ?p=preview - Plunkr 代码以查看问题所在并提出解决方案。

简单的 10-15 行代码,却花了我整个周末的时间来找出问题所在

最佳答案

来自 window.open 的文档:

Note that remote URLs won't load immediately. When window.open() returns, the window always contains about:blank. The actual fetching of the URL is deferred and starts after the current script block finishes executing. The window creation and the loading of the referenced resource are done asynchronously.

因此,请尝试将您的事件监听器包装在 window.setTimeout

function openInApp(){
var ref = window.open('http://google.com/','_blank','location=no');
window.setTimeout(function(){
ref.addEventListener('loadstart', function(event) {
console.log('in loadstart ');
alert('start: ' + event.url);
});
}, 1000);
}

假设您没有同源问题,现在应该可以了。

关于javascript - inapp 浏览器 window.open 不会触发 loadstart 或 loadstop 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38561260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com