/    Sign up×
Community /Pin to ProfileBookmark

Dom Scripting

Dear guys!

Im trying to figure out a problem with the most basic of javascript functions. Im trying to add the class “here” to each link in the navigation using javascript as illustrated by a book Im using. However, I seem to be missing something as the javascript isn’t doing its job! :injured:

Heres what Im talking about:

CSS:

[code=html]#navigation a.here:link,
#navigation a.here:visited,
#navigation a.here:hover,
#navigation a.here:active
{color: #eef;
background-color: #799;}[/code]

Javascript:

[QUOTE]

function highlightPage(){
if (!document.getElementsByTagName) return false;
if (!document.getElementById) return false;
if (!document.getElementById(“navigation”)) return false;
var nav = document.getElementById(“navigation”)
for (var i=0; i<links.length; i++){
var linkurl = links[i].getAttribute(“href”);
var currenturl = window.location.href;
if (currenturl.indexOf(linkurl) != -1){
links[i].className = “here”;
}
}
}

[/QUOTE]

For the code in its entirety, the test page is [URL=”www.javascript.net16.net”]here[/URL]
There could be a problem with the addLoadEvent function, but I can’t see jack!

(don’t make fun of the design)

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@brenz_netFeb 26.2010 — I think the problem is you're referring to a 'links' variable that doesn't exist, as far as I can tell... I didn't see it defined in your global.js called by your sample page either.

[CODE]
for (var i=0; i<links.length; i++){
[/CODE]
Copy linkTweet thisAlerts:
@kbedi87authorFeb 26.2010 — Thanks Brenz, you have a sharp eye!

However, I've tried to rectify the problem and its still not carrying out the function. :eek:

[code=html]
function highightPage() {
if (!document.getElementsByTagName) return false;
if (!document.getElementById) return false;
if (!document.getElementById("navigation")) return false;
var nav = document.getElementById("navigation");
var links = nav.getElementsByTagName("a");
for (var i=0; i<links.length; i++) {
var linkurl = links[i].getAttribute("href");
var currenturl = window.location.href;
if (currenturl.indexOf(linkurl) != -1) {
links[i].className = "here";
}
}
}
[/code]
Copy linkTweet thisAlerts:
@brenz_netFeb 26.2010 — You've misspelled the name of your function. In your global.js you have:

function highightPage() {
Copy linkTweet thisAlerts:
@kbedi87authorFeb 26.2010 — It was such a trivial mistake!

I bow to the great one before me.

Hope you have a great day, week, month, year and lifetime!:p
×

Success!

Help @kbedi87 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...