Solved

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

Posted on 2012-04-12
4
306 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 17

Expert Comment

by:Anuroopsundd
ID: 37841294
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

737 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