Javascript Exit Pop

Mike

New member
Jun 27, 2006
6,777
116
0
51
On the firing line
I'm sure there is a very simple answer to this, but I haven't found it yet. So, here goes:

This script does a great job of ignoring internal links and not popping when someone clicks on a link to leave the page, but it still pops on close regardless of whether they previously clicked an internal link. What I would like to do, is only have it pop on exit if they have not previously clicked a link.

Can someone help me out?

Here's the code:
Code:
<script type="text/javascript" language="javascript"> 
var areYouReallySure = false;
var internalLink = false;
function areYouSure() {
if (!areYouReallySure && !internalLink) {
areYouReallySure = true;
location.href="http://your-link-goes-here"
return "*****************************************************\n\nBefore you leave this page, blah blah blah blah blah\n\nPress **CANCEL** NOW to do something more\n\n*****************************************************";
}
}
window.onbeforeunload = areYouSure;
</script>
 


Does not look like you are setting var internalLink anywhere...

You'll need another piece of JavaScript to fire up and change that variable whenever internal link is clicked. You probably do not want to hardcode onClick property in every <A> tag, but you could add this behavior to every link by JS when the page loads. I am not sure if there is an easy way to do this for all links unless you are using a library like jQuery.

Here about onClick behaviors: Advanced JavaScript for Web Developers: onClick and onMouseOver - www.htmlgoodies.com
 
From what I understand, the script considers any relative link to be an internal link, and it seems to work that way. I'm trying to figure out a way to say something like:

Code:
if (isset($link) 
{
// do nothing
}
else
{
pop the exit pop
}

Yes, I realize that's not right exactly, but you get the idea (I hope)
 
You'll still have to catch earlier onClick event to put something into your IF statement. The only way I know how to do this is by attaching onClick handler to all links.
 
I'd put there in there:
jQuery
Code:
$('a').click(function(){

   internalLink = true;
   var newWindow = window.open($(this).attr('href'), '_blank');
   newWindow.focus();

});
 
Why would you open new window from .click function? Browser should be set to that anyways, you only need to set var internalLink for onbeforeunload...
 
Yeah you're right, it's not a total solution but showing what I would be trying to do. I also don't see you posting any code, just flapping your jaw a lot like you know what you're talking about.

I probably wouldn't use regular links. I'd probably use spans with the url inside it, something like:

Code:
<span class="link" url="http://google.com">Anchor</span>
And then access it like so:

Code:
$('span.link').click(function(){

   internalLink = true;
   var newWindow = window.open($(this).attr('url'), '_blank');
   newWindow.focus();

});
 
Yeah, you are so right. Code review has no value, so I won't do it. Ktxbai
 
Yeah, you are so right. Code review has no value, so I won't do it. Ktxbai

I'm not saying don't do it, but your not really bringing anything to the table. What I was trying to avoid was actually sitting down and writing the whole thing, but by request by SEO_Mike I ended up doing it anyways.

Code:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
$(document).ready(function(){
var areYouReallySure = false;
var internalLink = false;
function areYouSure() {
if (!areYouReallySure && !internalLink) {
areYouReallySure = true;
location.href="http://your-link-goes-here"
return "*****************************************************\n\nBefore you leave this page, blah blah blah blah blah\n\nPress **CANCEL** NOW to do something more\n\n*****************************************************";
}
}
window.onbeforeunload = areYouSure;

$('a').click(function(){

   internalLink = true;

});
});
    </script>
 
Thanks Rage! - This is definitely a helpful code snippet I could use.

Where's the donate button? :)