Popup message when leaving website

Discussion in 'HTML & Website Design' started by Matt_Baker, Oct 19, 2011.

  1. #1
    Good day!

    Please give me sample code on how to do popup message when someone leaves a website. Thanks :)

    Kind Regards,

    Matt
     
    Matt_Baker, Oct 19, 2011 IP
  2. shortcircuit

    shortcircuit Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Matt - give this a try:

    <script type = "text/javascript">
    var needToConfirm = true;
    window.onbeforeunload = confirmExit;
    function confirmExit() {
    if (needToConfirm) {
    return "You have attempted to refresh or 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>

    <input type="Submit" value="Save" onclick="needToConfirm = false;" />
     
    shortcircuit, Oct 20, 2011 IP
  3. terrypasencio

    terrypasencio Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This will generate a pop up message/alert before the page fully loads.....
    <script language="Javascript">
    alert ("HERE WRITE YOUR TEXT")
    </script>
     
    terrypasencio, Oct 20, 2011 IP
  4. Clive

    Clive Web Developer

    Messages:
    4,507
    Likes Received:
    297
    Best Answers:
    0
    Trophy Points:
    250
    #4
    Pretty useless piece of code, no offense, terrypasencio.

    Check this instead, a clean method:
    staying_in_site = false;
     
    Event.observe(document.body, 'click', function(event) {
      if (Event.element(event).tagName == 'A') {
        staying_in_site = true;
      }
    });
     
    window.onunload = popup;
     
    function popup() {
      if(staying_in_site) {
        return;
      }
      alert('I see you are leaving the site');
    }
    Code (markup):
     
    Clive, Oct 20, 2011 IP
  5. terrypasencio

    terrypasencio Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hello Clive,

    Heartly thanks to you for this suggestion..ya its really a clean method then what i recommended...
     
    Last edited: Oct 21, 2011
    terrypasencio, Oct 21, 2011 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    
    <body onbeforeunload='alert("please stay")'; >
    
    Code (markup):
    is a lot simpler.
     
    Rukbat, Oct 21, 2011 IP
  7. SOLOWPOET

    SOLOWPOET Active Member

    Messages:
    197
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    Will these codes work on Blogger as well If I insert them in the HTML/Javascript Widget or does it need to be inserted in the template?
     
    SOLOWPOET, Oct 21, 2011 IP
  8. Clive

    Clive Web Developer

    Messages:
    4,507
    Likes Received:
    297
    Best Answers:
    0
    Trophy Points:
    250
    #8
    Not to worry, all good :)

    Yes, and that will popup the message on every link click, even if it's an internal link.
    Site visitors will dump the website faster than one can imagine.
     
    Clive, Oct 21, 2011 IP
  9. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #9
    Is that another IE bug? If you use target="_blank", the JS doesn't fire in sane browsers.

    @SOLOWPOET:
    It needs to be on the actual page. If you can get your template to include it on the page, it'll work.
     
    Rukbat, Oct 21, 2011 IP