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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up warn User Whenever They Leave the browser in javascript??

    window.onbeforeunload = confirmExit;
    function confirmExit() {
    return "Please login or signup to save your images";
    }

    whenever i click on any link on website it shows dis message,

    i only wants to show message when user close the browser!!!

    plz suggets

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    JavaScript only knows if the page is about to be unloaded - it doesn't know why the page is being unloaded. It therefore can't distinguish between reloading the current page, going to another page on the same site, going to a page on a different site, or closing the current tab, or closing the browser.

    You might be able to set something from a click event on links in the page that you can test for from the beforeunload code to detect if the reason for the unload is that they clicked a link - that would get rid of some of the unloads not related to closing the browser.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,371
    Thanks
    11
    Thanked 591 Times in 572 Posts
    onbeforeunload will stop "popping up" in firefox and chrome soon, don't use it anymore.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Quote Originally Posted by rnd me View Post
    onbeforeunload will stop "popping up" in firefox and chrome soon, don't use it anymore.
    oh, crud. is there something else we can use?

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    ... in the meantime you could use a simple flag...

    Code:
    window.onbeforeunload = confirmExit;
    var waslink = false;
    
    function confirmExit() {
        if (!waslink) {
            return "Please login or signup to save your images";
        }
        waslink = false;
    }
    
    var lnks = document.getElementsByTagName("a");
    
    for (var i = 0; i < lnks.length; i++) {
        lnks[i].onmousedown = function () {
            waslink = true;
        }
    }


  •  

    Tags for this Thread

    Posting Permissions

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