views:

204

answers:

2

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.

+1  A: 

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.

geowa4
That's what I was looking for, thanks.
stu
+2  A: 

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.

Peter Bailey
probably shouldnt set onload like that. you are overriding whatever was there. use attachEvent or addEventListener, depending on your browser.
geowa4
I agree - I didn't really want to go through the complete abstraction process for this simple example - or do what I would *actually* do in a production system, use something like jQuery.
Peter Bailey
"know I can have the child window onload() to alert the parent, but I don't want to mess with the child" <- i dont know if your solution is what he wants. however, i definitely agree its the better way to do it.
geowa4
I though he was talking about putting a separate script block or include *into* the HTML for the child document - this gets around that. /shrug
Peter Bailey
well, between us, he has a few ideas. hope he takes yours though. GMV
geowa4