如何在新窗口加载完成后执行某些操作

如何在新窗口加载完成后执行某些操作

本文介绍了如何在新窗口加载完成后执行某些操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在完全加载后关闭一个新窗口。我有:

  my_window = window.open(http://www.yahoo.com,_blank, resizable = yes,scrollbars = yes,titlebar = yes,width = 500,height = 400,top = 10,left = 10); 

我试过了:

  my_window.onload = my_window.close(); 

  my_window.addEventListener('load',my_window.close(),true); 

等待my_window在关闭之前完成加载(它几乎立即关闭)。



第二个问题,我可以添加一个超时来继续并关闭新窗口,以防页面停顿并且由于某种原因从未完全加载?



 <$ c  $ c> my_window.onload = function(){my_window.close();}; 

或此:

  my_window.addEventListener('load',function(){my_window.close();},true); 

传递一个普通函数引用时,不要在函数名称后面使用parens。



我还添加了匿名函数,使得 my_window context可以保存到函数执行时间。



如果你的窗口不在同一个域中,那么你可能会遇到,以防止您访问其他窗口的javascript。


I need to close a new window after it fully loads. I have:

my_window=window.open("http://www.yahoo.com", "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=500, height=400, top=10, left=10");

I've tried:

my_window.onload = my_window.close();

and

my_window.addEventListener('load', my_window.close(), true);

Neither wait for my_window to finish loading before closing (it closes pretty much immediately).

Secondary question, can I add a timeout to go ahead and close the new window in case the page stalls and for some reason never fully loads?

解决方案

If your window is on the same domain, you can use this:

 my_window.onload = function() {my_window.close();};

or this:

my_window.addEventListener('load', function() {my_window.close();}, true);

You don't use parens after the function name when passing a plain function reference. Using the parens as you were doing causes the function to get executed immediately.

I also added anonymous functions so the my_window context can be preserved until function execution time.

If your window is not on the same domain, then you probably have issues with same origin protections that keep you from accessing the javascript of the other window.

这篇关于如何在新窗口加载完成后执行某些操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:58