Forum Moderators: open

Message Too Old, No Replies

Make links on a page inactive if you are on that page

inactive linke

         

hallion

9:36 am on Dec 8, 2004 (gmt 0)



Hi, first time post,
I need help with this as I am having trouble:

I have been asked to do the following:

The "menu" should provide links to all pages with a <div> tag and not change spatial appearance across all pages. The link to the page the user is on should become inactive.

The menu should be loaded into each page using a JS routine.

I am going bald with this as I am a relatively new scripter.

Help please anyone......

kaled

5:55 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need something like the following, but I'm away from home and can't check the details.

with (document) {
for (var i=0; i<links.length; i++) {
with (links[ i]) { if (href==location.href) disabled = true
}}}

I'm sure someone here can correct the mistakes. (I'm not certain that links is the correct name for the array and disabled=true might have to be replaced by enabled=false.)

Kaled.

Bernard Marx

1:19 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sure someone here can correct the mistakes

I'll see waht I can do, Kaled.

disabled/enabled aren't <a> attributes - and have no functional effect( although, funnily enough, the text does go a light shade of grey).

Myself, I'm a distruster of

with
, but this could be under the influence of hearsay. document.location is deprecated, yet I'll be married with grown up kids by the time support ceases for it. I went for the DOM route too, just to be oh-so up to date. (except for getting the href - no need to overdo it).


var anchs = document.getElementsByTagName('a'), link, k=0;
var loc = window.location.href;
while(anch=anchs[k++])
if(anch.href==loc)
anch.removeAttribute('href');

For pre-DOM browser support, document.links is the only (and easier) option, but I can't think how to deactivate a link. delete isn't allowed, and setting href to an empty string won't stop it being a link.

g1smd

8:30 pm on Dec 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



With Javascript, you're doing all the work in the browser, and it fails if Javascript is disabled.

If the site can use PHP, then you could do the work at the server end before the page is served out to the browser.