Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    onUnload not working properly...?

    Hey,

    I have a popup window which when closed, I want to call another function to then open another window.... I've used the following code in the first popup window...

    Code:
    <script language="JavaScript1.2">
    	function closeWindow() {
    	popup = window.open("leave.php","leave","height=50,width=50,scrollbars=no");
    	}
    </script>
    and the above function is called in the main body tag of the first popup..

    <body onUnload="closeWindow()">

    The reason for this is that when the user closes the first popup, I need to call a PHP script to end their session, I think this is the best way to do it... by opening a second window, calling the script and self.closing the window immediately... the problem is, the second window just isnt opening.. the first one closes without launching the second one...

    any help is much appreciated!

    thanks!!

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the problem is, the second window just isnt opening.. the first one closes without launching the second one...
    It always helps to indicate which browser in which your code doesn't work.

    It works fine for me in IE6.0 and Firefox 0.9.3.

    You can also write it:

    window.onunload = closeWindow;

    If you are using Firefox, are you sure that it isn't being blocked by the built-in popup blocker....?


    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    homer:

    i think you're fighting a losing battle with this... you can sorta do it for IE, but you can't for Moz.

    in IE, you've got the onbeforeunload event which get's called just before the browser window closes. with this you can call any extra functionailty you need for browser-session management. please note, however, as of XP-SP2, this may now be no longer working for the majority of your IE users, since SP2 blocks auto popups. i think it may do this without any ability to over-ride the behaviour where FF does for instance.

    this may, of course, if your site is actually an application for a closed audience system, such as an intranet, but it would be for a secure website, such as webmail account. in the case of the former, the weblication could be added to the trusted zone, thereby getting round any security restrictions. but you wouldn't be able to count on that for a website.

    you may have to figure out a way round the session issue, without having to use popups. as awkward as it is.
    *keep it simple (TM)

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem is that your window is closed before the onunload event can be fired....

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    thanks for the replies guys...

    It's a fairly closed audience, but still using the internet... not an intranet. What I am doing is for a client where they can edit a webpage in a WYSIWYG Editor, which opens in a new window.. a file is written to the server temp folder stating that this file is in use. If another user tries to edit the same file, it checks the "in use" files and will stop them editing it.

    The reason for the need to call a PHP script when the user closes the window is that I need to remove the filename (ie: call the PHP script) to "un lock" the file. It is actually calling the script, but the popup is not appearing...

    if anyone has any better ideas, I'd be v interested to hear them! Is there a way to remove the "X" close button of a window?? (probably unlikely?!)

    Hmm..just had an idea.... I could put onunload="return closeWindow()" then have the closeWindow function saying somethng like

    alert("please close this window using the button inside the page");
    return false;

    that might work?? Then I can have a button inside the popup which simply calls another PHP page... much easier!!

  • #6
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    just tried the above... and It does popup the alert, but then I get a message:

    "Are you sure you want to navigate away from this page?

    false

    Press OK to continue, or Cancel to stay on the current page"

    ps.. had to use "onbeforeunload=..." for it to work also....

    any way of getting rid of this message?? or forcing it to think you've pressed "cancel" ?

  • #7
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    ive also heard about "frameless" containers?? or something like that... ie: a popup with out any toolbars or buttons etc etc...... that might be an option?

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Try this:
    Code:
    function closeWindow() {
    	var popup = new Image();
    	popup.src = "leave.php?"+Date.parse(new Date()); //prevent caching
    }


  •  

    Posting Permissions

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