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.indexO f("/messag es/")) != -1)
{
window.open(document.links [i],"linkW indow",win dowprops,f alse);
now = new Date();
initTime = now.getTime();
delayTime = 1000;
for (wasteTime=initTime; wasteTime<(initTime+delayT ime); wasteTime++) {
now = new Date();
wasteTime = now.getTime();
status = i+"/"+(document.links.leng th)+" "+primitiveString;
}
}
}
function LinkList() {
windowprops = "menubars=yes,location=yes ,toolbars= yes,scroll bars=yes,r esizable=y es,"+"widt h=350,heig ht=400,top =50,left=5 0";
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
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.indexO
{
window.open(document.links
now = new Date();
initTime = now.getTime();
delayTime = 1000;
for (wasteTime=initTime; wasteTime<(initTime+delayT
now = new Date();
wasteTime = now.getTime();
status = i+"/"+(document.links.leng
}
}
}
function LinkList() {
windowprops = "menubars=yes,location=yes
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(
</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
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.li nks[i],"li nkWindow", windowprop s,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
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.li
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If all you care about is adding a URL to the history, why not check window.history.length before and after the window.open?
ASKER
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.g etElements ByTagName( "input")[0 ])) tagExists = true;
status = i+"/"+(document.links.leng th)+" "+primitiveString+" TagExists="+tagExists;
But the following script caused a script error when run:
while((childWindow.documen t.getEleme ntsByTagNa me("input" )[0])) == null)
{status = i+"/"+(document.links.leng th)+" "+primitiveString;}
Why does 'if' work but 'while' does not? I may have a syntax problem. Any ideas?
Thanks again,
Al Gordon
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.g
status = i+"/"+(document.links.leng
But the following script caused a script error when run:
while((childWindow.documen
{status = i+"/"+(document.links.leng
Why does 'if' work but 'while' does not? I may have a syntax problem. Any ideas?
Thanks again,
Al Gordon
ASKER
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.indexO f("/messag es/")) != -1) //Only visit links in 'messages' subdirectory
{
childWindow = window.open(document.links [i],"linkW indow",win dowprops,f alse); //Open link in childWindow
status = i+"/"+(document.links.leng th)+" "+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+delayT ime); wasteTime++) {
now = new Date();
wasteTime = now.getTime();
if((childWindow.document.g etElements ByTagName( "hr")[0]) != null) break; //If valid tag found in childWindow, then page has started to load
}
childWindow.document.write ("<html>An other 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=n o,scrollba rs=no,resi zable=nos, "+"width=5 00,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>
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.indexO
{
childWindow = window.open(document.links
status = i+"/"+(document.links.leng
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+delayT
now = new Date();
wasteTime = now.getTime();
if((childWindow.document.g
}
childWindow.document.write
}
}
function LinkList() { //Open all postings in childWindow to mark them as Visited
windowprops = "menubars=no,location=yes,
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(
</body>
Glad we could help. Thanks for the A. :^)
Cd&
Cd&
ASKER
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
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
You need to be able to reference the child, so open it this way:
x=window.open(document.lin
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&