views:

473

answers:

3

I have a script that creates a printable page by copying the HTML across and then doing some manipulation, such as disabling the buttons on the page, on page load.

I also want to disable the links on the page. I don't really mind if they look like links still as long as they don't do anything, and don't give any JavaScript errors! The anchor tag doesn't seem to have a disabled attribute...

Unfortunately, I can't use jQuery, so JavaScript only please!

Edit: I want to disable the links, buttons etc on the page so that when the 'printable page' opens in another window, the user cannot mess with it by clicking buttons or links. I want it to essentially be a 'frozen snapshot' of the page that they want to print.

+1  A: 

Setting their href to # and overwriting the onclick event should do the trick.

var links = document.getElementsByTagName("A"), j;
for (j = 0;j < links.length; j += 1) {
 links[j].href = '#';
 links[j].onclick = function () {
  return false;
 };
}
Matt
+1  A: 

Why can't you use jQuery? So much nicer...

$('a').each(function(){this.onclick=function(){return false}});

Anyway here is a normal javascript way. Smaller than above and you also don't need to modify the links... by defining the onclick function to return false it will not visit the href:

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

There is also an array of links in the document object. While I've never tried, I believe you can set them too.

for (i=0;i<document.links.length;i+=1) {
  document.links[i]=='#';
}
Mike Mengell