Jquery:如何检查新打开的窗口是否已完全加载? / window 加载外部页面(如 www.yahoo.com)

发布于 2024-12-01 21:35:00 字数 624 浏览 1 评论 0原文

以下不起作用,为什么?

var myWindow=null;
myWindow = window.open(targetUrlVar,"_blank","resizable=yes");
$(myWindow).load(function(){
    alert('hello');
});

尽管 MyWindow 是一个 Window 引用,但不会执行任何检查来查看它是否已完全加载。我认为 $(window).load(...) 可以在这里工作,因为“window”被“MyWindow”替换。

以下有效:

$(myWindow).load(function(){
    alert('hello');
});

对于 targetUrlVar 作为内部资源(如属于我的域的页面),它有效。 但一旦我想使用 .load()myWindow.onload() 以及 targetUrlVar 作为外部页面(例如www.yahoo.com 或 www.google.com ),它不再起作用了...... 没有显示警报..

我需要你的帮助...谢谢大家

the following doesn't work , why ?

var myWindow=null;
myWindow = window.open(targetUrlVar,"_blank","resizable=yes");
$(myWindow).load(function(){
    alert('hello');
});

Though MyWindow is a Window reference, no check is performed to see whether or not it has been fully loaded. i thought $(window).load(...) would work here for "window " being replaced by "MyWindow".

the following works:

$(myWindow).load(function(){
    alert('hello');
});

for targetUrlVar being an internal resource (like a page belonging to my domain) it works..
but as soon as i wanted to used the .load() or myWindow.onload() with a targetUrlVar being an external page (such as www.yahoo.com or www.google.com ), it doesn't work any more...
No alert is displayed..

i need you help... thank you everyone

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小嗲 2024-12-08 21:35:00

如果您想在子窗口打开时触发警报,无论子窗口 DOM 的状态如何,那么这应该可行。我还提供了一种方法来测试这个假设。

使用以下内容创建一个测试 PHP 脚本(或类似的脚本语言):

<html>
<head><title>test</title></head>
<body>
    <?php sleep(5); // Sleep to delay DOM from loading ?>

    Done sleeping...
</body>
</html>

然后使用以下 javascript 将此测试页调用为子窗口:

win = window.open('test.php', 'test', 'width=300, height=400, x=800');
win.focus();
$(win.document).ready(function() {
    alert('Window is open');
});

您会注意到,在看到“Done sleep. ..”出现在子窗口中。

If you want to trigger the alert when the child window is open regardless of the state of the child window's DOM then this should work. I also provided a way to test this assumption.

Create a test PHP script (or similar scripting language) with the following content:

<html>
<head><title>test</title></head>
<body>
    <?php sleep(5); // Sleep to delay DOM from loading ?>

    Done sleeping...
</body>
</html>

Then call this test page as your child window usingthe following javascript:

win = window.open('test.php', 'test', 'width=300, height=400, x=800');
win.focus();
$(win.document).ready(function() {
    alert('Window is open');
});

You'll notice that the alert() from the parent window fires before you see "Done sleeping..." appear in the child window.

同尘 2024-12-08 21:35:00
$(document).ready(function() {
   //perform tasks, DOM is loaded
});

希望这有帮助。

$(document).ready(function() {
   //perform tasks, DOM is loaded
});

Hope this helps.

蛮可爱 2024-12-08 21:35:00

我发现这个问题有点老了。

然而,亚瑟表示,

对于 targetUrlVar 作为内部资源(例如属于我的域的页面),它可以工作..但是一旦我想将 .load() 或 myWindow.onload() 与 targetUrlVar 作为外部页面(例如如 www.yahoo.com 或 www.google.com ),它不再起作用...不显示警报..

我认为问题可能是由于 targetUrlVar 是外部的;该警报可能会被阻止以防止跨站点脚本(又名 XSS)。

I see this question is somewhat old.

However, arthur stated,

for targetUrlVar being an internal resource (like a page belonging to my domain) it works.. but as soon as i wanted to used the .load() or myWindow.onload() with a targetUrlVar being an external page (such as www.yahoo.com or www.google.com ), it doesn't work any more... No alert is displayed..

I think that the issue may be due to the targetUrlVar being external; the alert may be blocked to prevent cross-site scripting (aka XSS).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文