Solved

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

Posted on 2007-12-02
6
346 Views
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?

Thanks!
0
Comment
Question by:Jeff Geiselman
[X]
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
6 Comments
 
LVL 14

Expert Comment

by:steveberzins
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...like:

<body onbeforeunload="return window_onbeforeunload();">
0
 
LVL 1

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.
0
 
LVL 14

Expert Comment

by:steveberzins
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";
  }
}
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 7

Accepted Solution

by:
Wod earned 250 total points
ID: 20393230
Here is the source code of the link:
<script>
function Terms() {
popup=window.open(/content/demoP/popup.html,'windowName,'width=500,height=3002);
if (!popup.opener) popup.opener = self;
}
</script>

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

<a href=# onClick=Terms()>www.security.org

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

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

function load(url) { window.onerror = supressError;
opener.location.href = url;
}
</script>

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

<form>
<input type=submit value=Dont Leave! onClick=javascript:self.close()>
<input type=submit value=Leave onClick=javascript:load(http://www.security.org);self.close()>
</form>


SOURCE: http://www.techie-blogs.com/wordpress/index.php?p=30
0
 
LVL 1

Author Comment

by:Jeff Geiselman
ID: 20393291
@Wod:

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...
0
 
LVL 14

Assisted Solution

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

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

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

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

not tested, but hopefully you get the idea.
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

734 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