This Tutorial Has Been Viewed 22,407 Times.
VN:F [1.9.20_1166]
Rating: 6.8/10 (22 votes cast)

This script will alert a message to the visitors before they move to another webpage or even try to cancel or close a webpage.This script confirms site exit with an alert box.
Here is the sample script.

<html>
<head>
<script type="text/JavaScript"> 
function ExitPage()
{
alert ('Before You Leave:\n\nPlease Check our new tutorials on http://www.mistonline.in.\n\nMore than 250 tutorials.\n\nPlease submit your own tutorials for free\n\nA complete reference for webmasters!!!!!.');
getit()
}
function getit() {
askit = confirm("Leaving so soon? Why not stay a little longer!");
if (askit == true)
alert('Thanks');
else
if (askit == false)
alert("Visit again soon!");
}
</script>
</head>
<body onbeforeunload="ExitPage();">

VN:F [1.9.20_1166]
Rating: 6.8/10 (22 votes cast)
Alert a message before leaving a web page using javascript, 6.8 out of 10 based on 22 ratings


coded by nessus
Categories: JavaScripts

6 Responses so far.

  1. Ankur
    reply

    Hi, Thnks for this amazing code.

    VA:F [1.9.20_1166]
    Rating: 0.0/5 (0 votes cast)
  2. prateek
    reply

    thanx ….but i dont want the alert box when people click on the links within the site …what to do for that ?

    VA:F [1.9.20_1166]
    Rating: 5.0/5 (1 vote cast)
    • admin
      reply

      What is your requirement then?

      VN:F [1.9.20_1166]
      Rating: 0.0/5 (0 votes cast)
  3. atrix
    reply

    how to make this only if a user i leaving site or closing browser, but not when he goes to a page within my site?

    VA:F [1.9.20_1166]
    Rating: 0.0/5 (0 votes cast)
    • admin
      reply

      You can create an alert box inside your website link using onclick attribute and javascript. Something like
      onClick=”javascript:alert(‘This is a test’)” , <a href=”" onClick=”javascript:alert(‘This is a test’)” >Test Link </a >
      I hope your issue has been resolved.

      VN:F [1.9.20_1166]
      Rating: 0.0/5 (0 votes cast)
  4. mygameagallery
    reply

    i want to popup a star rating window when after moving to another game (other game links ) in my website http://www.mygamegallery.com how can i achieve this.

    VA:F [1.9.20_1166]
    Rating: 0.0/5 (0 votes cast)

Leave a Reply