Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable all <a> links using javascript in Firefox and Chrome

I want to disable all href links in my page. i am using the below code and it is working fine only in IE.

var elems = document.getElementsByTagName("*");
    var len = elems.length;
    for (i=0; i<len; i++) {
           if(elems[i].tagName.toLowerCase() == 'a'){
               elems[i].disabled = true;           
           }
       }

but i want work it on all browsers. can somebody please help me how to overcome this issue.

Thanks in advance.

like image 576
ran Avatar asked Nov 28 '22 03:11

ran


1 Answers

If you don't want to change the href of the links, one could do this too:

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

Working demo here: http://jsfiddle.net/jfriend00/9YkJQ/.

like image 97
jfriend00 Avatar answered Mar 18 '23 14:03

jfriend00