Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to detect when a page has loaded in another window in Firefox

I was wondering what the best way was 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 don't mind if the solution requires a Firefox extension or Grease Monkey 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 doesn't 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>
like image 366
spiderplant0 Avatar asked Nov 14 '22 06:11

spiderplant0


1 Answers

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

like image 180
andrewmu Avatar answered Dec 09 '22 16:12

andrewmu