Link to home
Start Free TrialLog in
Avatar of Al_Gordon
Al_Gordon

asked on

How to track page loading progress in a child window from JavaScript in the parent window?

I am opening a child window from JavaScript located in the parent window, and I need to know when the loading of the child window has started. I have no control over the code in the child window, so I cannot make changes there. I have full control over code in the parent window that contains the JavaScript.

Here is code that I have used (it sort of works, but it just starts a page load in the child window once per second based on an array of URLs). What I need to know definitively is when the child window has started loading (because at that point, the URL of the child window will be written into the browser history, which is my only goal in opening the child window). Even a simple test that tells me when, say, more than 100 bytes have loaded into the child window would probably do the trick.

<head>....
function OpenLinkWindow() {
primitiveString = String(document.links[i]);
if((primitiveString.indexOf("/messages/")) != -1)
{
window.open(document.links[i],"linkWindow",windowprops,false);
now = new Date();
initTime = now.getTime();
delayTime = 1000;
for (wasteTime=initTime; wasteTime<(initTime+delayTime); wasteTime++) {
now = new Date();
wasteTime = now.getTime();
status = i+"/"+(document.links.length)+"  "+primitiveString;
}
}
}

function LinkList() {
windowprops = "menubars=yes,location=yes,toolbars=yes,scrollbars=yes,resizable=yes,"+"width=350,height=400,top=50,left=50";
self.name = "main";
for (i=0; i<document.links.length; i++) {OpenLinkWindow();}
alert("Close all windows and restart Internet Explorer");
}
</head>

<body>...
<a href="javascript:LinkList();">Mark all Postings as 'Visited'</a>
</body>

As you can likely tell from my code, I have just started teaching myself JavaScript (thank you David Flannagan) and my code probably leaves a lot to be desired.

If anyone knows how I can do this, I would be very appreciative.
Thanks for your help.
Al Gordon



Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

You have very little hope on this if you have no control of code on the pages in the child.

You need to be able to reference the child, so open it this way:
x=window.open(document.links[i],"linkWindow",windowprops,false);
x is now a handle to reference the child.  The only way you are going to be able to determine if the loading has started is if youcan reference something in the page.  using: if (x.document.variable) will return true if the variable named exists in the child, but that means you have to know somthing about the page.  You could also try if(x.document.body) or some other dom reference to test if it exists.  However I think there is probably one problem you will not be able to overcome.  If these page are not from the same domain as the parent, then you cannot refrence anything on them because built in security prevents it.

Cd&
Avatar of Al_Gordon
Al_Gordon

ASKER

Thank you, COBOLdinosaur! Based on your suggestion, I think the news is good, because:

1. All the links are to pages in the same domain; and
2. All the pages start with the same identifiable HTML code.

It looks to me like your idea of searching in 'x.document.body' to see if any predictable text has yet been loaded would be the answer.

As a beginner (1 week) in JavaScript, I am not sure how I would insert that logic into my JavaScript code. I assume I would:
1) initialize 'x' to some empty state,
2) execute 'x=window.open(document.links[i],"linkWindow",windowprops,false);', and
3) loop until known text at the start of the loading document (for example, "<head>") shows up. When the text appears, I would know that loading has started, and I could exit the loop.
4) Finally, I could embed this logic within the existing longer timeout in case some page is not there.

Am I on the right track? Could you offer any advice as to how this logic would be inserted into my code?

Thanks again,
Al Gordon


ASKER CERTIFIED SOLUTION
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If all you care about is adding a URL to the history, why not check window.history.length before and after the window.open?
COBOLdinosaur & pinnacle71,

Thanks for the ideas. I have been working on them for the past couple of hours, and I hope to get back to you tomorrow, either with a successful outcome or more questions.

One problem with using history.length is that if I am trying to load a page that is already in history, then length will not increment, and I will get stuck waiting for an increase in length. I guess I could also use timeout for those situations.

For some reason, history.length always remained stuck at 2, no matter how many entries there were in history.

Looking at another method based on COBOLd's suggestion, I ran into the following problem:

The following script worked:
tagExists = false;
if((childWindow.document.getElementsByTagName("input")[0])) tagExists = true;
status = i+"/"+(document.links.length)+"  "+primitiveString+"  TagExists="+tagExists;

But the following script caused a script error when run:
while((childWindow.document.getElementsByTagName("input")[0])) == null)
{status = i+"/"+(document.links.length)+"  "+primitiveString;}

Why does 'if' work but 'while' does not? I may have a syntax problem. Any ideas?

Thanks again,
Al Gordon
It works! Your suggestions pointed me in the right direction. Monitoring history.length was a good idea, but there were the problems I mentioned above. So the points go to COBOLdinosaur!

The code seems to be 100% reliable, and even uses a timeout to handle links that are corrupted or zero length.

This whole thing forced me to learn a lot about JavaScript and IE. Maybe I'll be able to offer some help soon!

Here is the code:


<head)...
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function OpenLinkWindow() { //Open the next link in 'messages' subdirectory
primitiveString = String(document.links[i]);
if((primitiveString.indexOf("/messages/")) != -1) //Only visit links in 'messages' subdirectory
{
childWindow = window.open(document.links[i],"linkWindow",windowprops,false); //Open link in childWindow
status = i+"/"+(document.links.length)+"  "+primitiveString; //Update browser status line for info only
now = new Date(); //Set timeout in case linked page is corrupted or zero length (and hence no tag found)
initTime = now.getTime();
delayTime = 5000; //Set timeout to 5 seconds
for (wasteTime=initTime; wasteTime<(initTime+delayTime); wasteTime++) {
now = new Date();
wasteTime = now.getTime();
if((childWindow.document.getElementsByTagName("hr")[0]) != null) break; //If valid tag found in childWindow, then page has started to load
}
childWindow.document.write("<html>Another posting marked as Visited!</html>");  //Clear last link out of childWindow
}
}

function LinkList() { //Open all postings in childWindow to mark them as Visited
windowprops = "menubars=no,location=yes,toolbars=no,scrollbars=no,resizable=nos,"+"width=500,height=200,top=50,left=50";
for (i=0; i<document.links.length; i++) {OpenLinkWindow();}
childWindow.close(); //Finished. Close childWindow, leave parent window open.
alert("Finished! Reload page in browser."); //Finished! Alert the user.
}

//  End -->
</script>    
</head>

<body>...
<a href="javascript:LinkList();">Mark all Postings Visited</a> (Internet Explorer only - runs for several minutes)
</body>
Glad we could help.  Thanks for the A. :^)

Cd&
If you would like to see the code in action, you can visit:
http://www.islet.org/forum/wwwboard.htm

Scroll to the bottom of the page where you will find the link "Mark all postings as Visited".

As you will see, this is a charitable medical foundation, so it was great to get the economical advice.

Thanks again,
Al Gordon