views:

440

answers:

4

I want to disable links in a page.

I used the function below, which is working in FF3 & IE7, but it is not working on IE6.

$(document).ready(function() {
    $("a").attr("href", "")
          .unbind("click")
          .click(function() { return false });
});
A: 

try

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; 
}

courtesy of http://radio.javaranch.com/pascarello/2005/05/17/1116355421179.html

gbrandt
Holy bejesus. A bit of overkill especially when jQuery is available, no?
Paolo Bergantino
@Paolo, haha, agreed
alex
+3  A: 

What about this?

$(document).ready(function() {
    $("a").removeAttr('href');
});
alex
muy bien. plus uno. :)
Paolo Bergantino
Oh come on that's basic spanish! "Very good. Plus one." =)
Paolo Bergantino
+1 on answer and comment :p
Christian Witts
Oh I wish I knew more language syntax :P Thanks Paolo.. let me try out this new +1 comment thing :)
alex
A: 

In some subversions of IE there is a bug which is the source of your problem. It ignores the onclick event's result. So what can you do? Well not a perfect way but IE6 also accepts "disabled" property for links. So try also setting the "disabled" property to ture. This will not effect other browsers.

BYK
A: 

Any click on an anchor tag generates a click event that bubbles up to body. The easiest thing would be to just put a click handler on body that does nothing and returns false if the target element is an anchor.

Russell Leggett