link code to prevent exit pop-up for internal

Discussion in 'JavaScript' started by lsargent, Aug 21, 2009.

  1. #1
    I was advised by a community member this post would be better suited for the JavaScript section (hope someone can help):

    The exit pop-up script I have is working fine, other than the fact that it engages on any internal links as well.

    Below I have the original script I was using along with a modified script sent back to me from someone in another forum. They also advised me to add rel="external" to all of my external links. I'm not sure if a different condition would apply whether the link stayed within my site or went elsewhere.

    In any event, it's still not working. The changes get me to my desired page on internal links, but the pop-up still engages and I have to click "OK" before it takes me there.

    I'm obviously looking to get this to work so that all internal links go straight to the page they're defined to, without the pop-up engaging at all.

    Both script versions are below:

    My original:
    
    <script type="text/javascript">
    var internal = 0;
    function bunload() {
    if (internal != 1) {
      window.onbeforeunload = null;
      alert("******************************************************\n WAIT!  WAIT!  WAIT!  WAIT!  WAIT!  WAIT!!  WAIT!!\n******************************************************\n\nMy partner will probably KICK MY *** for this...\n\nI am secretly giving away a LIMITED number of $20\ncoupons just for Tweeting about us.\n\nIf you act right now, you can grab our Manuscript\nplus ALL Bonuses for just $27!\n\nJust click *CANCEL* on the next window to get your\nV.I.P. discount link!\n\n******************************************************");
      window.location = "gift page";
      return "******************************************************\n  Click CANCEL  -  Click CANCEL  -  Click CANCEL  \n******************************************************\n\n  Click *CANCEL* Now to get your V.I.P. discount!\n\n"; }
    else {
      internal = 0;
           }
    }
    if (internal != 1) {
       window.onbeforeunload = bunload;
    }
       else {
       internal = 0;
    }
    </script>
    
    Code (markup):
    And the modified script another community member replied back to use:
    
    <script type="text/javascript">
    var internal = 0;
    function bunload() {
    if (internal != 1) {
      window.onbeforeunload = null;
      alert("******************************************************\n WAIT!  WAIT!  WAIT!  WAIT!  WAIT!  WAIT!!  WAIT!!\n******************************************************\n\nMy partner will probably KICK MY *** for this...\n\nI am secretly giving away a LIMITED number of $20\ncoupons just for Tweeting about us.\n\nIf you act right now, you can grab our Manuscript\nplus ALL Bonuses for just $27!\n\nJust click *CANCEL* on the next window to get your\nV.I.P. discount link!\n\n******************************************************");
      window.location = "gift page";
      return "******************************************************\n  Click CANCEL  -  Click CANCEL  -  Click CANCEL  \n******************************************************\n\n  Click *CANCEL* Now to get your V.I.P. discount!\n\n"; }
    else {
      internal = 0;
           }
    }
    if (internal != 1) {
       window.onbeforeunload = bunload;
    }
       else {
       internal = 0;
    }
    window.onload = function()
    {
    	var links = document.getElementsByTagName('a');
    	for(i = 0 ; i < links.length; i++)
    	{
    		if(links[i].getAttribute('rel') == 'external')
    		{
    			links[i].onclick = function()
    			{
    				bunload();	
    			}
    		}
    	}
    }
    </script>
    
    Code (markup):
    Any suggestions on what I'm doing wrong here? My apologies, but my coding isn't very strong and I'm just picking this stuff up as I go.

    Thanks
     
    lsargent, Aug 21, 2009 IP
  2. JavaScriptBank.com

    JavaScriptBank.com Peon

    Messages:
    141
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    very cool & good code, thank you very much for sharing, but have a live demo?

    Can you share the perfect code on my JavaScript library?


    Awaiting your response. Thank
     
    JavaScriptBank.com, Aug 24, 2009 IP