如何查看子浏览器窗口是否已完成加载?

时间:2021-03-10 07:14:35

I have a link on a page that pops up a new window, and sometimes that second window takes a long time to load. I want to disable the link on the parent window until the child window finishes loading. I know I can have the child window onload() to alert the parent, but I don't want to mess with the child, so I have a little javascript that polls the child window to see if it's got any data, except that it doesn't work. I can't newwindow.document.getelementbyid because I can never be sure there's going to be any particular element on that page. I just want to see if there's a in it somewhere.

我在页面上有一个链接弹出一个新窗口,有时第二个窗口需要很长时间才能加载。我想禁用父窗口上的链接,直到子窗口完成加载。我知道我可以让子窗口onload()来提醒父母,但是我不想弄乱孩子,所以我有一个小的javascript来轮询子窗口以查看它是否有任何数据,除了它不起作用。我不能newwindow.document.getelementbyid因为我永远无法确定该页面上是否会有任何特定元素。我只是想知道某个地方是否有它。

2 个解决方案

#1


You could try getting the body tag, and inspecting if it has children. If it does then the page is either loaded or very close. Close enough IMO.

您可以尝试获取body标签,并检查它是否有子项。如果是,则页面加载或非常接近。足够接近IMO。

If the child's URL is not the same domain, you can't do anything. That would be a XSS vulnerability.

如果孩子的URL不是同一个域,则无法执行任何操作。这将是一个XSS漏洞。

#2


Just add a listener to the load event of the child window. Here's a really basic example

只需在子窗口的load事件中添加一个监听器即可。这是一个非常基本的例子

<html>
<head>
<script type="text/javascript">

onload = function()
{
  document.getElementById( 'test' ).onclick = function( event )
  {
    event.preventDefault();
    event.returnValue = false;
    var link = event.target || event.src;
    var childWin = window.open( link.href, 'child', 'width=400,height=300' );
    childWin.onload = function()
    {
      alert( 'child window loaded!' );
    }
  }
}

</script>
</head>
<body>

<a href="test2.html" id="test">test</a>

</body>
</html>

NOTE: I should tell you that this only works if the URL you are loading in the child belongs to the same domain as the parent. The browser security model prevents you from doing so otherwise.

注意:我应该告诉您,这只适用于您在子项中加载的URL属于与父项相同的域。浏览器安全模型会阻止您这样做。

#1


You could try getting the body tag, and inspecting if it has children. If it does then the page is either loaded or very close. Close enough IMO.

您可以尝试获取body标签,并检查它是否有子项。如果是,则页面加载或非常接近。足够接近IMO。

If the child's URL is not the same domain, you can't do anything. That would be a XSS vulnerability.

如果孩子的URL不是同一个域,则无法执行任何操作。这将是一个XSS漏洞。

#2


Just add a listener to the load event of the child window. Here's a really basic example

只需在子窗口的load事件中添加一个监听器即可。这是一个非常基本的例子

<html>
<head>
<script type="text/javascript">

onload = function()
{
  document.getElementById( 'test' ).onclick = function( event )
  {
    event.preventDefault();
    event.returnValue = false;
    var link = event.target || event.src;
    var childWin = window.open( link.href, 'child', 'width=400,height=300' );
    childWin.onload = function()
    {
      alert( 'child window loaded!' );
    }
  }
}

</script>
</head>
<body>

<a href="test2.html" id="test">test</a>

</body>
</html>

NOTE: I should tell you that this only works if the URL you are loading in the child belongs to the same domain as the parent. The browser security model prevents you from doing so otherwise.

注意:我应该告诉您,这只适用于您在子项中加载的URL属于与父项相同的域。浏览器安全模型会阻止您这样做。