Javascript Code for Alert Box on Page Close

8 replies
Here's an example when the page is closed: Dave Guindon's Exit Splash | Exit Page Software | Increase Sales

I could buy that script, but it's overpriced because it has extra unnecessary features.

All I'm after is the basics. What javascript snippet can create a custom alert message when a page is closed to then open another page? Ideally, I don't want the solution so you have to edit every link out on the page with some javascript onclick:false, for example.
#alert #box #close #code #javascript #page
  • Profile picture of the author andrejvasso
    I didnt test this snippet, but if i didnt make a mistake, it should create a pop up on all kinds of page exit.

    Code:
    <script language="javascript">
    
    function exitPopUp()
    {
    alert ("hi");
    }
    
    </script>
    
    <body onUnload="exitPopUp()">
    wdawd
    </body>
    I just saw that you want to redirect visitors after they clicked on okay. so you would have to change the alert function to a confirm function. like this:

    Code:
    <script language="javascript">
    
    function exitPopUp()
    {
        var answer = confirm("Alert message !")
        if (answer){
            window.location = "http://www.google.com/";
        }
        else{
            alert("Cancel it !")
        }
    }
    
    </script>
    
    <body onUnload="exitPopUp()">
    wdawd
    </body>
    {{ DiscussionBoard.errors[3903087].message }}
    • Profile picture of the author Joshua Uebergang
      Thanks for trying Andre.

      The code you provided only creates an alert message when the window is closed. If you click cancel, "Cancel it" as text in another alert box comes up. No new window opens.
      {{ DiscussionBoard.errors[3911772].message }}
      • Profile picture of the author Joshua Uebergang
        Just discovered there's an event: window.onbeforeunload

        I don't know how to use it for what I'm after, but I hope someone can. Here's what I have scrapped together so far (I know jack all javascript):

        <html>
        <head>
        <script type="text/javascript">
        <!--
        ok=1;
        function closeIt(url,name)
        {
        return "Wait! This is an alert message.";
        if (ok) new_window = window.open('http://www.example.com','example', ' menubar,resizable,dependent,status, width=300,height=200,left=500,top=10')
        }
        window.onbeforeunload = closeIt;
        // -->
        </script>
        </head>
        <body>
        wdawd
        </body>
        </html>
        {{ DiscussionBoard.errors[3911855].message }}
        • Profile picture of the author PayalRastogi
          Originally Posted by Joshua Uebergang View Post

          Just discovered there's an event: window.onbeforeunload

          I don't know how to use it for what I'm after, but I hope someone can. Here's what I have scrapped together so far (I know jack all javascript):

          <html>
          <head>
          <script type="text/javascript">
          <!--
          ok=1;
          function closeIt(url,name)
          {
          return "Wait! This is an alert message.";
          if (ok) new_window = window.open('http://www.example.com','example', ' menubar,resizable,dependent,status, width=300,height=200,left=500,top=10')
          }
          window.onbeforeunload = closeIt;
          // -->
          </script>
          </head>
          <body>
          wdawd
          </body>
          </html>
          I am always face critical problem in Java script for creating flash.
          {{ DiscussionBoard.errors[9325103].message }}
  • Profile picture of the author Dhira
    Can anyone confirm if this still works?
    {{ DiscussionBoard.errors[9322260].message }}
  • Profile picture of the author Alex Athey
    Whoa...talk about a necro thread! Since you brought it up, yes, it does work. You can test Javascript without ever having to load it on a web server: on your local computer open up Notepad or its Mac/Linux/whatever equivalent and copy and paste the code.

    Then, save it to your documents as "test.html". Double click on test.html and your default browser should load it up. Attempt cancelling, and you'll see the popup. I tested it in two minutes, works as expected.

    Fair warning: it's a default Javascript popup look, and these days people expect popups to have a little more style in them.
    {{ DiscussionBoard.errors[9324660].message }}
  • Profile picture of the author PayalRastogi
    I want to know that what is basic different of page.html and page.htm, is this effected on the flowing of website speed.
    {{ DiscussionBoard.errors[9325101].message }}

Trending Topics