1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Prompt user when leaving page

Discussion in 'JavaScript' started by Om ji Kesharwani, Jan 10, 2011.

  1. #1
    I have this code to prompt user when leaving page
    SEMrush
    <script language="JavaScript">
    var needToConfirm = true;

    window.onbeforeunload = confirmExit;
    function confirmExit()
    {
    if (needToConfirm)
    return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
    }
    </script>

    But i need some modification that user should not prompt when navigate to internal pages.

    User should prompt in case navigate away from my site to external site link.
     
    Om ji Kesharwani, Jan 10, 2011 IP
    SEMrush
  2. Freeman93

    Freeman93 Active Member

    Messages:
    139
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #2
    thanks. May need this on the future.
     
    Freeman93, Jan 10, 2011 IP
  3. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #3
    Try such
    <a href="http://www.externallink.com" onclick="alert('Please do not run from my website'); return false;">Download</a>

    href="the external link"
    onclick="here goes the JS; return false;"
    return false for the disabling of clicking. So they can one "Right Click & Save of OPEN in new window"

    its easy :D
     
    Tanya Roberts, Jan 12, 2011 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    You could just set needtoconfirm to false whenever an internal link or button is clicked.

    Although surely there would be a much more efficient way to achieve this, rather then adding code to every internal link or button.
     
    camjohnson95, Jan 13, 2011 IP
  5. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #5
    ^and how do you do that?
     
    Tanya Roberts, Jan 13, 2011 IP
  6. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    <a href="internalpage.html" onclick="needToConfirm = false; return true;">My link</a>
     
    camjohnson95, Jan 14, 2011 IP
  7. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #7
    here is a better solution:
    
    <a href="http://www.google.com">Google</a>
    <a href="internal1.html">Internal link 1</a>
    <a href="internal2.html">Internal link 2</a>
    <script type="text/javascript">
    var needToConfirm = true;
    
    window.onbeforeunload = confirmExit;
    
    function confirmExit()
        {
            if (needToConfirm)
            return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
        }
    
    var links = document.getElementsByTagName("a");
    for(i=0;i<links.length;i++) {  
        if(links[i].getAttribute("href").search("http://")==-1){
            links[i].onclick=function() {
                needToConfirm = false;
                return true;
            }
        }
    }
    </script>
    
    Code (markup):
    This will automatically stop internal links from displaying the alert.
     
    camjohnson95, Jan 14, 2011 IP
  8. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #8
    I liked that.
     
    Tanya Roberts, Jan 15, 2011 IP