Solved

How to Add a Warning when Navigating to an External Website?

Posted on 2012-04-12
4
309 Views
Last Modified: 2012-06-27
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!
0
Comment
Question by:mfranzel
[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
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
basicinstinct earned 500 total points
ID: 37841289
you could do this in javascript...

attach a click event handler on the body element of each html page

each click check if the event target is an anchor (you will actually need to check if it's inside an anchor too)

if it is an anchor then check the href - if the href is on a different domain to the current page (window.location.host) then show your message and cancel the event or let it thru as necessary
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 37841319
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,
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?");
            };
        }
    }
};

Open in new window

0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37841324
i made a start for you... runs in chrome on linux that's all i checked...

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;
        }
    }
})();
¿

Open in new window

0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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