Click here to Skip to main content
12,633,764 members (32,249 online)
Click here to Skip to main content
Add your own
alternative version

Stats

16.2K views
4 bookmarked
Posted

Disable all 'links' on the page via Javascript

, 25 Feb 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
While working on one of the feature asked by client to disable all the controls of the panel while an asynchronous operation is in process, I encountered that though link buttons are disabled(grayed out) they can still be clicked (thus they still work!) :(I was looking into it why such...
While working on one of the feature asked by client to disable all the controls of the panel while an asynchronous operation is in process, I encountered that though link buttons are disabled(grayed out) they can still be clicked (thus they still work!) :(

I was looking into it why such behaviour and came across the link:
Disable All Links with JavaScript[^]

In order to disable all of the links on the page, we need to add an onclick on all the link buttons and return false. If there is an onclick handler on that, we need to grab it and add it to the link.

Here is the code snippet for it:
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; 
} 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sandeep Mewara
Software Developer (Senior)
India India
Current location: Bangalore, India.

You may also be interested in...

Comments and Discussions

 
GeneralGood One Pin
thatraja29-Oct-10 21:57
mvpthatraja29-Oct-10 21:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 25 Feb 2010
Article Copyright 2010 by Sandeep Mewara
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid