views:

43

answers:

1

Hi, I was wondering what is the best way to detect when a page has finished loading where the page in question is in a different tab or window. And where page to be loaded could be any page on the web and is not under my control.

I am using Firefox only and I dont mind if the solution requires a firefox extension or greasemonkey script (I have already added the configuration/commands to allow access to pages from different domains).

The code below shows an attempt. It sometimes detects when the initial page has loaded but when I press 'submit' to reload a new page into the other window, it doesnt detect it.

Thanks.

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

    currentPageObj = window.open("http://www.bbc.co.uk", "currentPage");

    currentPageObj.addEventListener("load", function(){alert("loaded")}, false);

</script>
</head>
<body>

    <form action="http://www.example.com" target="currentPage">
         <input type="submit">
    </form>

</body>
</html>
+1  A: 

In the other page just have an onload event handler which calls a method in the parent window (window.opener) to do what you want. e.g. in the child page:

window.addEventListener('load', function () {
  if (window.opener && !window.opener.closed)  {
     window.opener.childLoaded();
  }
}, false);

And have childLoad do what you want in the parent window. (Note: it might be best to keep a reference to the window you want to deal with e.g. initialise var parentWindow = window; because when you call between windows this can get confusing.)

andrewmu
Thanks andrewmu, but I forgot to mention that in the other window I'm loading a page from a different domain that I dont have control of and therefore I cant edit this page.
spiderplant0
Ok.. that is tricky then. Unless you do some clever proxying through your server and insert the appropriate response into the page on it's way back. E.g. put the above into a script tag that gets insert before the </body> of the response before you send it to the browser.
andrewmu