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
TrackBacks[0]
Comments[12]
Posted by pascarello on May 17, 2005 1:43:41 PM EST
how can I disable only the links in a certain div?
Comment from Anonymous on October 21, 2005 10:07:56 AM EST
Chnage the objLinks statement to this:
objLinks = document.getElementById("divID").getElementsByTagName("a");
Comment from Eric Pascarello on October 21, 2005 10:33:07 AM EST
Your script saved our asses. Thanks, man!
Comment from Anonymous on November 10, 2006 5:02:12 PM EST
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.
Comment from Anonymous on November 12, 2006 10:33:33 AM EST
You've saved me! Thanks!
works perfectly the first time... which is saying a lot.
Comment from Mark on June 13, 2007 12:35:00 PM EST
Thanx a lot for ur help!
Comment from Chetan Sharma on March 26, 2008 8:07:17 AM EST
THANKS A LOT!
Comment from Hrushikesh on April 19, 2008 5:07:29 AM EST
Yep, great little script..specially the for specific links.
Comment from Anonymous on June 18, 2008 3:42:22 PM EST
Really i have to thank you.....you have saved me from 1 week work to 1 hour work
Comment from Ramki on September 3, 2008 11:21:48 AM EST
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.
Comment from Arpan on September 11, 2008 8:59:17 AM EST
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);
Comment from Buttau on November 14, 2008 5:12:27 AM EST
TrackBack to http://radio.javaranch.com/pascarello/addTrackBack.action?entry=1116355421179
/####################################
var linkArray = new Array();
function disablelinks(){
var objLink = document.links;
for(var i=0;i < objLink.length;i++) {
linkArray[i] = objLink[i].href.toString();
if (objLink[i].id=='dwA1') {
//this just allows me to skip some
}
else {
objLink[i].disabled=true;
objLink[i].onclick = new Function("return false;");
}
}
}
function enablelinks() {
var objLink = document.links;
for(var i=0;i < objLink.length;i++) {
objLink[i].disabled=false;
objLink[i].href=linkArray[i];
objLink[i].onclick = linkArray[i];
}
}
/####################################