A little popup help please!

Discussion in 'JavaScript' started by sabian1982, Nov 5, 2013.

  1. #1
    Ok,

    I'm trying to tweak some JS code that will open a popup when the visitor tries to exit the page.

    When the user tries to exit the page they get a popup however at the same time I'd like the page behind the popup to automatically redirect to a specified URL.

    Any suggestions please?

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        function PopIt() { return "Are you sure you want to leave?"; }
        function UnPopIt()  { /* nothing to return */ }
        $(document).ready(function() {
            window.onbeforeunload = PopIt;
            $("a").click(function(){ window.onbeforeunload = UnPopIt; });
        });
    </script>
    Code (markup):
     
    sabian1982, Nov 5, 2013 IP
  2. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    
    <script type="text/javascript">
    function PopIt { alert("Are you sure you want to leave?");
    window.location = "Redirect URL Here";
    }
    
    $(document).ready(function() {
    window.onbeforeunload = PopIt;
    }
    </script>
    
    Code (markup):
    This should work but I'm rather tired so maybe not.
     
    Pudge1, Nov 8, 2013 IP
  3. karthikmllore

    karthikmllore Active Member

    Messages:
    132
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    53
    #3
    may be you can try using .unload() of jquery.... You'll have to use a older version of jquery though (<1.8)... I don't think it'll work, but worth a try...
     
    karthikmllore, Nov 28, 2013 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4
    I don't think that you can do this. If the user wants to leave they will leave. You can set a message to confirm they want to leave but not redirect them.
     
    HuggyStudios, Nov 28, 2013 IP
  5. lastminuteworks07

    lastminuteworks07 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    This is definitely possible with a llittle tweak in the code uploaded by you. The message can definitely be set but redirecting them to the respective page as defined certainly needs a good hand at programming.
     
    lastminuteworks07, Dec 4, 2013 IP
  6. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #6
    Which programming would accomplish this? I'm sure browsers do not allow you to redirect a user when they try to leave.
     
    HuggyStudios, Dec 4, 2013 IP