mfranzel
asked on
How to Add a Warning when Navigating to an External Website?
Hello everyone,
I am working on a website for a client. I am editing their current site that was build by someone else so I am still "learning the code".
Basically, my client wants it so that when a user clicks on a link on the site that takes them AWAY from the company's site, it displays a warning message notifying the user that they are leaving the site.
I know this is extremely annoying, but thats what my client wants. Does anyone know how I can add something like this so it happens "automatically", meaning, I don't have to create a special page for every external link.
Is there a way for some type of PHP script of something to detect when a user clicks a link to an external site it displays the message?
Thanks!
I am working on a website for a client. I am editing their current site that was build by someone else so I am still "learning the code".
Basically, my client wants it so that when a user clicks on a link on the site that takes them AWAY from the company's site, it displays a warning message notifying the user that they are leaving the site.
I know this is extremely annoying, but thats what my client wants. Does anyone know how I can add something like this so it happens "automatically", meaning, I don't have to create a special page for every external link.
Is there a way for some type of PHP script of something to detect when a user clicks a link to an external site it displays the message?
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://stackoverflow.com/questions/2365994/display-a-warning-when-leaving-the-site-not-just-the-page
Observe that you will need JavaScript in order to display the confirmation message, so you may as well do the entire thing in JavaScript.
For example,
For example,
window.onload = function()
{
for (var i = 0; i < document.links.length; i++)
{
var lnk = document.links[i];
if (lnk.href.indexOf(window.location.hostname) === -1)
{
lnk.onclick = function()
{
return confirm("Are you sure you want to leave?");
};
}
}
};
i made a start for you... runs in chrome on linux that's all i checked...
http://jsfiddle.net/6NXyD/5/
http://jsfiddle.net/6NXyD/5/
(function(){
var container = document.getElementById("container");
if(container.addEventListener)
{
container.addEventListener("click", clickEvent, false);
}
else
{
container.attachEvent("onclick", clickEvent);
}
function clickEvent($event)
{
$event = $event || self.event;
var element = $event.target || $event.srcElement;
while(element)
{
if(/^a$/i.test(element.tagName))
{
if(element.href.indexOf(window.location.origin) !== 0)
{
if(!confirm("really want to go away?"))
{
if($event.preventDefault)
{
$event.preventDefault();
}
else
{
$event.returnValue = false;
}
}
};
break;
}
element = element.parentNode;
}
}
})();
¿