Weird Thoughts From Eric's Head

Tags - Categories : All | AJAX | BUSINESS | PERSONAL | PROGRAMMING | BOOK REVIEW

Disable All Links with JavaScript
Another question that has popped up is how do I disable all of the links on the page? Well you can loop through and set the links to disabled. That in return makes them gray, but it does not disbale them.
To disable the link, you need to add an onclick and return false. If their is an onclick handler on that, you need to grab it and add it to the link. So much fun!

window.onload= function(){
        DisableEnableLinks(true)
}

function DisableEnableLinks(xHow){
  objLinks = document.links;
  for(i=0;i<objLinks.length;i++){
    objLinks[i].disabled = xHow;
    //link with onclick
    if(objLinks[i].onclick && xHow){  
        objLinks[i].onclick = new Function("return false;" + objLinks[i].onclick.toString().getFuncBody());
    }
    //link without onclick
    else if(xHow){  
      objLinks[i].onclick = function(){return false;}
    }
    //remove return false with link without onclick
    else if(!xHow && objLinks[i].onclick.toString().indexOf("function(){return false;}") != -1){            
      objLinks[i].onclick = null;
    }
    //remove return false link with onclick
    else if(!xHow && objLinks[i].onclick.toString().indexOf("return false;") != -1){  
      strClick = objLinks[i].onclick.toString().getFuncBody().replace("return false;","")
      objLinks[i].onclick = new Function(strClick);
    }
  }
}

String.prototype.getFuncBody = function(){ 
  var str=this.toString(); 
  str=str.replace(/[^{]+{/,"");
  str=str.substring(0,str.length-1);   
  str = str.replace(/\n/gi,"");
  if(!str.match(/\(.*\)/gi))str += ")";
  return str; 
} 
Hope that helps!

Eric Pascarello
Moderator of HTML/JavaScript at www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint for Dynamic Web Pages


I found it less troublesome to add each links href to an array and then just restore the value But I never would have gotten it without your help
/####################################
var linkArray = new Array();
function disablelinks(){
&nbsp;&nbsp;&nbsp;&nbsp;var objLink = document.links;
&nbsp;&nbsp;&nbsp;&nbsp;for(var i=0;i < objLink.length;i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;linkArray[i] = objLink[i].href.toString();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (objLink[i].id=='dwA1') {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//this just allows me to skip some
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objLink[i].disabled=true;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objLink[i].onclick = new Function("return false;");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
}
function enablelinks() {
&nbsp;&nbsp;&nbsp;&nbsp;var objLink = document.links;
&nbsp;&nbsp;&nbsp;&nbsp;for(var i=0;i < objLink.length;i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objLink[i].disabled=false;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objLink[i].href=linkArray[i];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objLink[i].onclick = linkArray[i];
&nbsp;&nbsp;&nbsp;&nbsp;}
}
/####################################
how can I disable only the links in a certain div?
Chnage the objLinks statement to this:
objLinks = document.getElementById("divID").getElementsByTagName("a");
Your script saved our asses. Thanks, man!
Just want to let you know that using FF 1.5 this script causes problems with <a> tags with no onClick functions asigned. The workaround: change the line objLinks[i].onclick = function(){return false;} you have to enter function(){return false;} quoted. Otherwise there will be problems with FF.
You've saved me! Thanks! works perfectly the first time... which is saying a lot.
Thanx a lot for ur help!
THANKS A LOT!
Yep, great little script..specially the for specific links.
Really i have to thank you.....you have saved me from 1 week work to 1 hour work
Hi Eric, Your approach work fine and It disables hyperlinks however the links on my 'printer friendly page' come up with 'hand' symbol while hvering over links. This is because the link is inside an anchor tag. Is there any way to remove anchor tags of a page without actually parsing the page and removing anchor tags manually ?? My page contains a good number of hyperlinks within anchor tags and If i try to remove them manually (by parsing and doing a string replacement, it degrades performance of my page). Please help.
Thanks a lot! But there is a problem. If code in onclick uses standard parameter 'event', the script causes an error. Here is a workaround (there must be an event-param, change 2 lines): 1: objLinks[i].onclick = new Function("event", "return false;" + objLinks[i].onclick.toString().getFuncBody()); 2: objLinks[i].onclick = new Function("event", strClick);


Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 9 + 1 = (Helps stop blog spam)
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).

TrackBack to http://radio.javaranch.com/pascarello/addTrackBack.action?entry=1116355421179