Skip to content Skip to sidebar Skip to footer

How To Disable All Links Before Load Jquery?

I need a pure Javascript code to disable all links while loading the page, avoiding to click them and redirecting to other page when its using a Facebox. Can be something that I pl

Solution 1:

Make this the first script tag in your head section:

window.onclick = function(e) {
    if(typeof $ === 'undefined') {
            e = e || window.event;
        e.preventDefault();
    }
}

This catches all click events, and if $ is undefined, cancels the event.

Has the advantage of working even before the DOM is loaded.

Solution 2:

Use document.links and prevent their default behaviour with preventDefault().

var links = document.links;
for (var i = 0, length = links.length; i < length; i++) {
    links[i].onclick = function(e) {
        e = e || window.event;
        e.preventDefault();
    }
}

If you wanted a more permanent disabling of the links, use removeAttribute('href').

Place this code somewhere after the closing body tag if you don't want to use DOMContentLoaded or the windowload event.

You can't place it in the head element otherwise and have it execute immediately because document.links will most certainly be empty.

Solution 3:

In the page, give all your links empty href values. Use the load event to assign appropriate values. Users with javascript disabled or unavailable will see broken links.

Post a Comment for "How To Disable All Links Before Load Jquery?"