How can I make a popup to warn users they are about to leave the site?

Posted on 2007-12-02
Last Modified: 2008-02-01
I am looking for some sort of Javascript that will initiate when the user clicks on a link pointing outside of the domain. The popup would have some security information and also contain a link to accept or decline the webpage from opening.

Does anyone have any idea? If its not possible to detect an external link, then how do I do this by designating the onclick() for a link?

Any ideas?

Question by:Jeff Geiselman
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 14

Expert Comment

ID: 20393138
you can use the .onbeforeunload to detect when your page is about to be unloaded, which basically pops a confirm dialog.

function window_onbeforeunload() {
  return "You are leaving my site and if you do, you may regret it, so click cancel unless you know what you're doing, if you click OK and leave, you do it at your own risk.";
<frameset onbeforeunload="return window_onbeforeunload();">

this is on a frameset, but I'm pretty sure can do the same on a normal html document

<body onbeforeunload="return window_onbeforeunload();">

Author Comment

by:Jeff Geiselman
ID: 20393153
This will happen anytime a page is loaded. I will have a list of links, some internal and some external. I only want the popup to occur when the user clicks on an external link.
LVL 14

Expert Comment

ID: 20393222
Well, then you will probably need to be able to determine the difference somehow, and put code in one or the other, to either turn the prompt off for your links, or on for the others, or maybe both just to assure the prompt works as expected.

var exitPrompt = false/true;
function window_onbeforeunload() {
   if (exitPrompt) {
    return "You are leaving ....blah blah";
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

Wod earned 250 total points
ID: 20393230
Here is the source code of the link:
function Terms() {,'windowName,'width=500,height=3002);
if (!popup.opener) popup.opener = self;

Hi there - below youll see a link to When you click it, a popup will appear that you need to accept to follow the link.

<a href=# onClick=Terms()>

Or you can see it in its own page here: view the source for index.html

Here is the source of the popup:
function supressError() { return true; }

function load(url) { window.onerror = supressError;
opener.location.href = url;

Warning you are about to leave my cool site,
only click Leave if you absolutely want too! :)

<input type=submit value=Dont Leave! onClick=javascript:self.close()>
<input type=submit value=Leave onClick=javascript:load(;self.close()>


Author Comment

by:Jeff Geiselman
ID: 20393291

That code works fine and all, but that would mean a seperate popup.html for every external link on my webpage because in the popup the link is defined, not from what was clicked.

That problem defeats the purpose. I want one popup.html with accept or decline buttons that will redirect based upon what link was clicked...
LVL 14

Assisted Solution

steveberzins earned 250 total points
ID: 20393361
cool stuff Wod.

so pass the link on the querystring.
 in links:
<a href=# onClick=Terms("");></a>

function Terms(url) {'/content/demoP/popup.html?' + escape(url), 'windowName,'width=500,height=3002);
if (!popup.opener) popup.opener = self;

in popup
<input type=submit value=Leave onClick="javascript:load('" + unescape(document.location.query) + "');self.close();">

not tested, but hopefully you get the idea.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question