stu
stu

Reputation: 8805

How can I check to see if a child browser window has finished loading?

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.

Upvotes: 2

Views: 5291

Answers (2)

Peter Bailey
Peter Bailey

Reputation: 105878

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

<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.

Upvotes: 3

geowa4
geowa4

Reputation: 41823

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.

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

Upvotes: 2

Related Questions