SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Attaching unload listener to a new window that redirects

    Okay, here's the issue:
    A page I have full control of is launching a new window (via JavaScript, naturally). This new window immediately redirects; I have no control over either of these pages.

    Here's what I want to do:
    I want to set an event that will fire when this new window closes that will redirect the parent window. In other words:
    Window A opens Window B
    Window B redirects to another page
    Window B closes, causes Window A to redirect

    Under normal circumstances, I could attach an unload listener to Window B that would set window.opener.location to the desired URL; however, the redirect in Window B causes the unload event to fire immediately, which is not what I want. Any help would be appreciated!
    PHP questions? RTFM
    MySQL questions? RTFM

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So the second page loaded in Window B is on another domain? If so, you are out of luck.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need control over Window B, you can use something like this:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var googleWin;
    var googleTimeout;
    var googleWait = 500;
    
    function openGoogle() {
    	googleWin = window.open("http://www.google.com","googleWindow");
    	checkGoogleOpen();
    }
    function checkGoogleOpen() {
    	if (googleWin && googleWin.closed) {
    		alert("Window was closed, do something");
    		window.clearTimeout(googleTimeout);
    	} else {
    		window.setTimeout(checkGoogleOpen, googleWait);
    	}
    }
    </script>
    </head>
    <body>
    <button onclick="openGoogle();">Google</button>
    </body>
    </html>

  4. #4
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Kravvitz - Nyet, the second page is not on a separate domain from the first, and in fact I just discovered that, contrary to what I posted earlier, it is in fact a refresh only.

    @jimfraser - Jim, you rock! I was just starting to think in this direction, but hadn't gotten as far as you. Just to confirm, the delay set in the second parameter of setTimeout is in milliseconds, correct? So your example checks every half second, right?
    PHP questions? RTFM
    MySQL questions? RTFM

  5. #5
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good idea, Jim.

    kromey, yes, it runs every half second.

    I've heard that setInterval() is more efficient than setTimeout().
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  6. #6
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup, I've heard the same, and I ended up adapting Jim's solution to make it work in my particular situation, taking the liberty of substituting setInterval for setTimeout in the process.
    PHP questions? RTFM
    MySQL questions? RTFM

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad I could help


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •