Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 611
  • Last Modified:

Hide All Links On Page

How can I hide all links on a page?

I know that it involves using...

document.getElementsByTagName("a")

Is there a way for the script to thoroughly search for, and hide all links no matter what a link's "class" or "id" is?

Thank you very much for your help!
0
ironsamurai
Asked:
ironsamurai
  • 5
  • 3
2 Solutions
 
ruscompCommented:
Maybe something like the following...
aLinks = document.getElementsByTagName("a");
var i=0;
for (i=0;i<=aLinks.len;i++)
{ 
aLinks[i].style.display="none";
}

Open in new window

0
 
ruscompCommented:
Or if you didn't want the element to collapse, you could use:

.style.visibility="hidden";
0
 
ironsamuraiAuthor Commented:
Thanks, that's what I had in mind, but it doesn't seem to work.

Here's what I have so far:

<html>

<head>

<title>Test</title>

</head>

<body>

<script>

aLinks = document.getElementsByTagName("a");
var i=0;
for (i=0;i<=aLinks.len;i++)
{
aLinks[i].style.display="none";
}

</script>

<a href="#">Remove this link</a>

</body>

</html>



Also, is there a way for the script to search for all links if the actual script is below a link? For example:

<a href="#">Remove this link</a>

<script src="thescript.js" type="text/javascript"></script>


Thanks again!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ruscompCommented:
I beleive the script should always be after the links... javascript will fire off as it is encountered, so if it is at the top of the page (right below opening body tag or in the head tag), there are no links that exist yet... so it won't see them...

One option is create the script as a function, and then add it to the body onload event.

function hideLinks {
 aLinks = document.getElementsByTagName("a");
 var i=0;
 for (i=0;i<=aLinks.len;i++)
 {
 aLinks[i].style.display="none";
 }
}

And then in the body tag:
onload="hideLinks();"
0
 
ironsamuraiAuthor Commented:
It still isn't working, here's what I have:


<html>

<head>

<title>Test</title>

</head>

<body onload="hideLinks();">

<a href="#">Remove this link</a>

<script>

function hideLinks {
 aLinks = document.getElementsByTagName("a");
 var i=0;
 for (i=0;i<=aLinks.len;i++)
 {
 aLinks[i].style.display="none";
 }
}

</script>

</body>

</html>


Any idea what's wrong?

Thanks!
0
 
ruscompCommented:
Is the browser showing any errors?
0
 
ironsamuraiAuthor Commented:
Yeah, IE is saying "Object Expected" and Opera is saying "Syntax Error".
0
 
David S.Commented:
I expect you mean the length property.

It's best to avoid using inline event handlers, especially on <body>.
function hideLinks() {
  var aLinks = document.getElementsByTagName("a");
  for(var i=0,k=aLinks.length; i<k; i++) { 
    aLinks[i].style.display="none";
  }
}
window.onload = function(){
  hideLinks();
}

Open in new window

0
 
ruscompCommented:
Woops, I do my best not to mix up languages :)  With .length it should be perfect...
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now