04 July 2013

Message presented you leave your site

This time,I present in JavaScript (Jquery) code that checks whether you leave your domain. If so - popup alert to informing you that you are leaving the domain

$(document).ready(function () {
    $("a").click(function(event) {
        // check if the target link is your.domain.com
        // if not will show alert message
        if (this.href.indexOf("your.domian.com") == -1) {
            if (this.href.startsWith("http")) {
                event.preventDefault();
                var linkLocation = this.href;
                alert('Note that you are directed to an external site does not belong to this site');
                window.open(linkLocation, '');
                return false;
            }
        }
    });

});


Of course, change the code from "your.domian.com" to your site.

thanks,
Roi