Skip navigation

Monthly Archives: August 2012


It is good practice to warn users while navigating away from one domain to another. How to achieve this using jQuery?

The code snippet below demonstrate this

$("a[href^='http://']:not([href*='"+location.hostname+"']), [href^='https://']:not([href*='"+location.hostname+"'])")
    .attr("target","_blank")
    .click(function(e) {
        alert('You are leaving this website and going somewhere else');
    });

Hope this helps!!

Credit goes to Alex Staveley and open web.