6 replies
Hi warriors,

I have set up a wordpress blog and am looking for a piece of code, piece of software or possibly even a wordpress plugin that will allow me to do the following:

When a user closes their browser or presses the back button on their browser I want them to be redirected to another page with a message that asks whether they would like to leave or stay on the page. For example purposes, the following site has this property - Backlink Profit Monster - Powerful Link Building Software.

Anyone know how this can be achieved? Any replies would be really appreciated.

Thanks,
Ali
#coding #question
  • Profile picture of the author PRBandit
    I found this

    Code:
    <script language="JavaScript">
      var needToConfirm = true;
      
      window.onbeforeunload = confirmExit;
      function confirmExit()
      {
        if (needToConfirm)
          return "Are you sure you want to exit this page?";
    window.location="link";
      }
    </script>
    Just google search if you want to find other scripts that do more for you.
    {{ DiscussionBoard.errors[4372873].message }}
    • Profile picture of the author aliduncan
      Originally Posted by PRBandit View Post

      I found this

      Code:
      <script language="JavaScript">
        var needToConfirm = true;
       
        window.onbeforeunload = confirmExit;
        function confirmExit()
        {
          if (needToConfirm)
            return "Are you sure you want to exit this page?";
      window.location="link";
        }
      </script>
      Just google search if you want to find other scripts that do more for you.
      Thanks for the speedy reply.

      I actually found this exact code when I ran a google search myself. Having tried it out, it does display a message asking whether the user wants to stay on the page but unfortunately does not redirect them to another page at the same time like the example page above.

      I searched a few times on google and couldn't find anything that managed to do both of these things simultaneously.

      Do you, or any other warriors, have a piece of code that can do this?
      {{ DiscussionBoard.errors[4372918].message }}
  • Profile picture of the author mojojuju
    Originally Posted by aliduncan View Post


    When a user closes their browser or presses the back button on their browser I want them to be redirected to another page with a message that asks whether they would like to leave or stay on the page.
    I'm pretty sure that if the user closes their browser or presses the back button, it means that the user wants to leave.
    Signature

    :)

    {{ DiscussionBoard.errors[4372986].message }}
    • Profile picture of the author PRBandit
      Originally Posted by mojojuju View Post

      I'm pretty sure that if the user closes their browser or presses the back button, it means that the user wants to leave.
      Haha, true.

      But anyways, heres the code

      Code:
      <script>window.onbeforeunload = function(){
              location.assign('http://www.google.com');
              return "go to google instead?";
          }
      </script>
      Make sure you leave the return line with a short message

      I tested this and used it in the <body> tags
      {{ DiscussionBoard.errors[4374077].message }}
    • Profile picture of the author aliduncan
      Originally Posted by mojojuju View Post

      I'm pretty sure that if the user closes their browser or presses the back button, it means that the user wants to leave.
      Haha, if they're that intent on leaving then they'll ignore the offer that I'm giving them and click on 'leave the page' when it appears. I'm sure you've seen this code used by marketers before.

      Originally Posted by PRBandit

      Code:
      <script>window.onbeforeunload = function(){ location.assign('http://www.google.com'); return "go to google instead?"; }</script>

      Make sure you leave the return line with a short message

      I tested this and used it in the <body> tags
      Fantastic! Thats just what I needed.

      Really appreciate the help.
      {{ DiscussionBoard.errors[4374310].message }}
      • Profile picture of the author mojojuju
        Originally Posted by aliduncan View Post

        I'm sure you've seen this code used by marketers before.
        I have, and it's a nuisance.
        Signature

        :)

        {{ DiscussionBoard.errors[4374315].message }}

Trending Topics