dennismonsewicz Posted December 18, 2008 Share Posted December 18, 2008 Can Javascript communicate between two windows using a heart beat monitor or some kind of counter? I need for a var to continually look at the main windows URL and if it changes to save that new url to the var. Any ideas on this? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 18, 2008 Share Posted December 18, 2008 if a child window is opened by a parent window, you can access the parent window from the child window with: window.opener the url can be obtained with: window.opener.window.location.href if the parent's url changes to something on a different domain, you will get a permission denied error just put the above code inside a setInterval() command Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted December 18, 2008 Author Share Posted December 18, 2008 ok so i have this code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>JS Testing</title> <script type="text/javascript"> function checkUrl() { var oldurl = window.opener.window.location.href; setInterval(document.write(oldurl), 5000); } </script> </head> <body onload="checkUrl();"> <p>Test 1</p> </body> </html> it displays the URL it came from but it doesn't check to see if the parent window has changed... any thoughts? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted December 18, 2008 Author Share Posted December 18, 2008 updated code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>JS Testing</title> <script type="text/javascript"> //setInterval("checkUrl()", 500); function checkUrl() { var oldurl = window.opener.window.location.href; document.write(oldurl); } </script> </head> <body onload="checkUrl()"> </body> </html> Is there anyway to have the page reload so it displays the URL of the parent window? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted December 19, 2008 Author Share Posted December 19, 2008 bump Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 19, 2008 Share Posted December 19, 2008 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>JS Testing</title> <script type="text/javascript"> var url; var timer; function checkUrl() { if(url != window.opener.window.location.href){ clearInterval(timer); //Stop the timer //Put code that you want run here alert('The parent URL changed'); } } function init(){ url = window.opener.window.location.href; //Save the current parent url timer = setInterval("checkUrl()", 500); //Start the timer } </script> </head> <body onload="init()"> </body> </html> what do you mean by: Is there anyway to have the page reload so it displays the URL of the parent window? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted December 19, 2008 Author Share Posted December 19, 2008 I updated your code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>JS Testing</title> <script type="text/javascript"> var url; var timer; function checkUrl() { if(url != window.opener.window.location.href){ clearInterval(timer); //Stop the timer //alert('The parent URL changed'); } } function init(){ url = window.opener.window.location.href; //Save the current parent url timer = setInterval("checkUrl()", 500); //Start the timer document.write(url); //I added this line } </script> </head> <body onload="init()"> </body> </html> The page is not refreshing.. like i need the child (pop-up/under) window to refresh every like 5 seconds and checks to see if the parent window has changed and if it has then display that url on the child page Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted December 19, 2008 Author Share Posted December 19, 2008 bump Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 20, 2008 Share Posted December 20, 2008 the popup doesn't need to refresh. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>JS Testing</title> <script type="text/javascript"> var url; var timer; function checkUrl() { if(url != window.opener.window.location.href){ document.write('URL changed to: '+window.opener.window.location.href+'<br>'); url = window.opener.window.location.href; } } function init(){ url = window.opener.window.location.href; //Save the current parent url timer = setInterval("checkUrl()", 500); //Start the timer } </script> </head> <body onload="init()"> </body> </html> Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted December 22, 2008 Author Share Posted December 22, 2008 hmmm we are getting closer... I loaded your code and tested it. Is there anyway to have the current parent URL printed on the page once you go to the newly created child page and when the parent URL changes it re-prints the url on the child page? also once the timer starts the child page just keeps trying to load. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 22, 2008 Share Posted December 22, 2008 the 'child page just keeps trying to load' is caused by using document.write(). instead, let's just append it to a div: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>JS Testing</title> <script type="text/javascript"> var url; var timer; function checkUrl() { if(url != window.opener.window.location.href){ url = window.opener.window.location.href; document.getElementById('output').innerHTML += 'URL changed to: '+url+'<br>'; } } function init(){ url = window.opener.window.location.href; //Save the current parent url document.getElementById('output').innerHTML = 'URL started at: '+url+'<br>'; timer = setInterval("checkUrl()", 500); //Start the timer } </script> </head> <body onload="init()"> <div id="output"></div> </body> </html> Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted December 22, 2008 Author Share Posted December 22, 2008 this is awesome! It works! One last thing.. is there anyway to append the data over and over in the same div without writing the url down the page? so it only shows up once? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 22, 2008 Share Posted December 22, 2008 this is awesome! It works! One last thing.. is there anyway to append the data over and over in the same div without writing the url down the page? so it only shows up once? i don't understand what you mean Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted December 22, 2008 Author Share Posted December 22, 2008 at the current time the code writes the new url on the page everytime the parent url changes. And continues down the page. Is there a way to just write it once and every time the url changes write the new url? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 22, 2008 Share Posted December 22, 2008 ah...so instead of appending, erase the old text and just show the new text? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>JS Testing</title> <script type="text/javascript"> var url; var timer; function checkUrl() { if(url != window.opener.window.location.href){ url = window.opener.window.location.href; document.getElementById('output').innerHTML = 'URL changed to: '+url+'<br>'; } } function init(){ url = window.opener.window.location.href; //Save the current parent url document.getElementById('output').innerHTML = 'URL started at: '+url+'<br>'; timer = setInterval("checkUrl()", 500); //Start the timer } </script> </head> <body onload="init()"> <div id="output"></div> </body> </html> p.s. all i did was remove the + in front of the = on this line: document.getElementById('output').innerHTML = 'URL changed to: '+url+'<br>'; Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted December 22, 2008 Author Share Posted December 22, 2008 yeah... the code you posted is still appending and not erasing i REALLY appreciate your help bud! Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 22, 2008 Share Posted December 22, 2008 uh...nope...check again Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted December 22, 2008 Author Share Posted December 22, 2008 yeah you definitely ROCK! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.