How to get javascript in one frame to wait until a page has loaded in a second frame

Keir Finlow-Bates picture Keir Finlow-Bates · Aug 5, 2009 · Viewed 10.7k times · Source

I have two frames in a frameset - frame[0] contains a script that loads a page into frame[1] using

top.frames[1].location.href = 'http://some_location.com/page.html';

and then performs actions on that page, for example searching text within the page. I need the script to wait until page.html has loaded in the second frame before doing this, and I can't use onload=... in the second page because I have no control over its source.

Is there a way to do this?

Answer

palindrom picture palindrom · Aug 5, 2009

use onload event of FRAME element

edit:

<frame onload = "if( top.frames[1].location.pathname == '/page.html' " ) alert( 'loaded' )";

or if you load different pages to the same frame use this:

<frame onload = "if( top.frames[1].location.pathname != 'about:blank' " ) alert( 'loaded' )";

or

<frame src = '/dummyInitial.html' onload = "if( top.frames[1].location.pathname != '/dummyInitial.html' " ) alert( 'loaded' )";