SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fire onbeforeunload once and then disable

    Hi,

    I want to fire onbeforeunload event for once on my page and then disable it. I tried the following code but it doesn't work. Any ideas about this?

    HTML Code:
    var counter = 0;
    window.onbeforeunload = function() {
    	if (counter === 0) {
    		return false;
    	}
    	counter++;
    }
    Purpose: That will be used to bust iframe buster.

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Can you give a use case? What is the scenario in which you would need to bust an iFrame buster?

    Are you trying to do it from the framed page or the page that is doing the framing?
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,834
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by nayen View Post
    Purpose: That will be used to bust iframe buster.
    The value to be returned is the text to display in the dialog to ask if they have changed their mind. Returning false will allow the page to unload without asking.

    So with the code you have it allows the page to be unloaded without asking the first time. The rest of your code never runs because the page is unloaded when it gets to the return false and never gets to run counter++ at all.

    Only if you replace the return false on the first call with a message offering them the alternative of not reloading the page will the counter ever get incremented. Then you'd need to return false to stop the message appearing on subsequent calls so that it will automatically unload on the second call.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    The value to be returned is the text to display in the dialog to ask if they have changed their mind. Returning false will allow the page to unload without asking.

    So with the code you have it allows the page to be unloaded without asking the first time. The rest of your code never runs because the page is unloaded when it gets to the return false and never gets to run counter++ at all.

    Only if you replace the return false on the first call with a message offering them the alternative of not reloading the page will the counter ever get incremented. Then you'd need to return false to stop the message appearing on subsequent calls so that it will automatically unload on the second call.
    Thank you Stephen, your suggestion helped me modify the code and finally find the one that works. I will share it here in case someone else needs the same thing in the future:

    Code:
    var bust = 0;
    window.onbeforeunload = function() {
    	if (bust === 0) {
    		bust++;
    		return 'Stay on this page.';
    	}
    }
    Now, the alert is displayed only on the first attempt from the iframed page that tries to open itself in the top window. I need one more thing about this code. I want to make this code active only within a limited timeframe. For example it will become inactive after 3 or 5 seconds. Is something like that possible?

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,834
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The following line of code should overwrite that event handler after three seconds:

    Code:
    setTimeout(function() {window.onbeforeunload = null;},3000);
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    The following line of code should overwrite that event handler after three seconds:

    Code:
    setTimeout(function() {window.onbeforeunload = null;},3000);
    Thank you very much Stephen.


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
  •