SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member jmneter's Avatar
    Join Date
    Jul 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to open a window when the user close the current page?

    I want to use the window.top.location='URL' in the body's event onunload(),but this statement seems not to be executed.

    But,if I add an alert('') method after the statement window.top.location, the statement works oddly!

    If I use the window.open(),it works.

    Why? Can anybody tell me?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,228
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    Are you certain that the alert isn't showing before the window unloads? Or is the window actually going to the new page and not unloading?

    Or is the window going to that location just before it then closes.

  3. #3
    SitePoint Member jmneter's Avatar
    Join Date
    Jul 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, the alert shows twice, before and after opening the window.
    I've made some testing, I find the window isn't going to the new window.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,228
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    I tried several variations of code - none worked. My guess is that the unload event negates the location property before that line runs, but the alert is "more browser than page" so it shows.
    Not a definate explanation of why "location" doesn't work, but at least you know "open" does work.

  5. #5
    SitePoint Member jmneter's Avatar
    Join Date
    Jul 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes,it seems the open is my only choice.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    You can use the onbeforeunload event, cancel that event so that the unload doesn't occur, then change the page location.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •