...

View Full Version : Help with simple script



four0four
06-20-2008, 02:54 AM
Hi,

I'm trying to fix a script that does a search for all links on a page and applies a display:none style to each one.

Here's what I have so far:



<html>

<head>

<title>Test</title>

</head>

<body>

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

<script>

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

</script>

</body>

</html>



When loading the script in IE it says:

alink[...].style is null or not an object


Does anyone know how to fix this?


Thanks!

binaryWeapon
06-20-2008, 03:49 AM
Instead of using javascript to search for every link and then hide each one, why not just use


<style type="text/css">
a:link {
display:none;
}
</style>


However, the solution to your problem is:

for (i=0; i<=alink.length; i++){should be
for (i=0; i<alink.length; i++){

This actually threw an error in FF as well, but FF just plowed on ahead and rendered it anyway. IE has a habit of not executing that part at all if there is an error.

rangana
06-20-2008, 03:53 AM
You missed on adding a type attribute.
YOu need to declare alink and i.

I'm unaware where you want this be triggered, I suppose it's onload:


<script type="text/javascript">
window.onload=function()
{
var alink = document.getElementsByTagName('a');
for (var i=0; i <alink.length; i++)
{
alink[i].style.display = "none";
}
}
</script>


Highlighted are added.

Hope it helps.

binaryWeapon beat me.

four0four
06-25-2008, 09:01 PM
Thanks! It works perfectly now! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum