October 5, 2012

What is the best way to make entire web page links non clickable

Question by Codemator

Possible Duplicate:
Override default behaviour for link ('a') objects in Javascript

What is the best way for making entire page links in home page read only (non clickable like href=# or href="javascript:void()" based on a user action.

I wanted to use only Javascript and CSS to achieve this.

Answer by Veli

try

with jquery

$("a").click(function() { return false; });

vanilla js

        var elements = document.getElementsByTagName("a");
        for (var i = 0; i < elements.length; i++) {
            elements[i].onclick = function () { return false; }
        }

Answer by Starx

Here is a wonderful solution by CMS, by using event delegation technique.

document.onclick = function (e) {
  e = e ||  window.event;
  var element = e.target || e.srcElement;

  if (element.tagName == 'A') {
    someFunction(element.href);
    return false; // prevent default action and stop event propagation
  }
};

Demo

...

Please fill the form - I will response as fast as I can!