Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

link manipulation - Netscape 2

Status
Not open for further replies.

RobBroekhuis

Technical User
Oct 15, 2001
1,971
US
I wrote the following script to manipulate the links on my page according to a user-initiated setting change:
Code:
function ShowHelp(state) {
   var helptext = document.getElementById("help");
   helptext.style.display=(state)?"block":"none";
   var helptext = document.getElementById("nohelp");
   helptext.style.display=(state)?"none":"block";
   window.document.forms[0].hidehelp.value=1-state;
   for (var i=0; i<window.document.anchors.length; i++) {
            var link=window.document.anchors[i];
            if (state) {
               link.href=link.href.substr(0,link.href.length-11);
            } else {
               if (link.href.indexOf('?')==-1)
                  {link.href+=&quot;?hidehelp=1&quot;;} else
                  {link.href+=&quot;&hidehelp=1&quot;;}
            }
   }
}
Works fine in IE, but not in Netscape. Which object references must I change to get it to work in Netscape?


Rob
[flowerface]
 
At first it looked like you forgot to identify where you were in the array with the , but that's the TGML. Anyway...

I've never messed with this before, so I'm not sure of the distinction between the anchors array and the links array...they are both valid arrays. However, the anchors array length was giving me 0 in the test code I put together, so I changed it to the links array and got it to work in IE6 and Netscape7.

for(var i=0; i < document.links.length; i++) {
link = document.links;
// ...
}

Kevin
 
Rob,

Using getElementsByTagName would be a more compliant way of doing things... You never know when these exposed arrays (links, anchors) are going to be hidden:

Code:
function ShowHelp(state)
{
	var helptext = document.getElementById(&quot;help&quot;);
	helptext.style.display=(state)?&quot;block&quot;:&quot;none&quot;;
	var helptext = document.getElementById(&quot;nohelp&quot;);
	helptext.style.display=(state)?&quot;none&quot;:&quot;block&quot;;
	window.document.forms[0].hidehelp.value=1-state;

	var myAnchorsArray = document.getElementsByTagName('a');
	for (var i=0; i<myAnchorsArray.length; i++)
	{
		var link = myAnchorsArray[ i ];
		if (state)
			link.href = link.href.substr(0, link.href.length-11);
		else
		{
			if (link.href.indexOf('?') == -1) link.href += '?hidehelp=1';
				else link.href += '&hidehelp=1';
		}
	}
}

Hope this helps!

Dan
 
I tried Kevin's suggestion to replace &quot;anchors&quot; with &quot;links&quot;, and indeed it now works on both browsers. Kinda funny, because the javascript online info I reference is Netscape-centric, yet it was Netscape that didn't like the anchors object. Dan - thanks for the additional insights you provide as well. In my case, I just need something that works for my particular small application, but I'll keep your more elaborate approach in mind for future applications.


Rob
[flowerface]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top