Forum Moderators: open

Message Too Old, No Replies

Highlighting Bookmarked Areas not working in FF

Works ok in IE not in FF

         

rutabaga

8:51 pm on Aug 22, 2009 (gmt 0)

10+ Year Member



This code works fine in IE6; it highlights the linked paragraph & unhighlights it when another link is clicked and hightlights the newly clicked link. But it does not work in FF3.5 (or even 2). In the script, the line, currentTarget.className = 'highlighted'; shows an error of "null" value. I've tried to figure this out on my own, but can't find why it doesn't have the value it is supposed to have like IE. I would sure appreciate it if you could see why this code works fine in IE, but not in Mozilla. Thanks.

This is the html code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>

<link rel="stylesheet" type="text/css" href="bookstore1.css" />
<script type="text/javascript" src="maincontent.js" ></script>

</head>

<body>

<div id="header">
<!-- <img alt="" src="images/BooksInPrintPlusShad.png" width="368" height="60"
class="booksInPrintpng" /><br /> -->
&nbsp;
<div id="scatleaImage">
<h2>Scattered Leaves</h2>
</div>
<p class="styleBookName">by J. D. Roque</p> <br />
<div id="chapterSamples">Samples of Stories
<div id="storyNames">
<ul id="chapters">
<li><a href="#The Word">The Word</a></li>
<li><a href="#What Goes Around">What Goes Around</a></li>
</ul>
</div>
</div>
</div>

<div id="maincontent">
<h2 class="aboutStories">Samples</h2>

<p id="The Word" class="content"><strong><em> The Word:&nbsp; </em></strong>
&quot;At our house, the penalty for uttering <em>the word</em> is to get your mouth
washed out with the yellow Fels Naphtha soap Ma uses when she mops the wood
floors.<br />
&nbsp;&nbsp;&nbsp; &quot;Erik--he's my older brother--used to say it all the time, so
he had the cleanest teeth amoung any of us kids.&quot;</p>

<p id="What Goes Around" class="content"><strong><em> What Goes Around:&nbsp; </em></strong>
"Although Archibald Diggs departed the company of this bar pals in a well
developed state of ineriation, he could not shake the feeling that he was
pedaling away from something very much amiss back there behind The Yellow
Ribbon Bar &amp; Grill.&nbsp; He had covered less than a mile when curiosity
brought him to a stop against a bus-stop bench.&nbsp; He closed his eyes,
trying to get his thoughts together.&nbsp; <em>What if it isn't...just some
hobo looking to bed down in the weeds?&nbsp; What if it is a terrorist come
to blow Greendale to hell and back right off the map...what if...?"</em> </p>
</div>
</body>
</html>

This is the javascript:

function addNormalClass() {
this.className = 'content';
}

function highlightTarget() {
// Collect the relevant items:
var contentsArticles = document.getElementById('maincontent').getElementsByTagName('p')
// Loop through them:
for (var j=0;j<contentsArticles.length;j++) {
if (contentsArticles[j].className == 'highlighted') {
// If one of them has a class name of 'highlighted,' change it to 'normal':
contentsArticles[j].className = 'content';
}
}
// What is the url of the clicked link?
var url = this.href;
// Get everything after the '#' in the link--that's our id
var elementId = url.substring(url.lastIndexOf('#') + 1);
// Get the element:
var currentTarget = document.getElementById(elementId);
// Change its classname:
currentTarget.className = 'highlighted';
}

function buildContentsArray() {
// First collect all the links to contents and the contents they link to:
var contentsLinks =
document.getElementById('chapters').getElementsByTagName('a');
var contentsArticles =
document.getElementById('maincontent').getElementsByTagName('p');

// Loop through the links:
for (var i=0;i<contentsLinks.length;i++) {
// Call a function when the links are clicked AND when a highlighted article is clicked:
contentsLinks[i].onclick = highlightTarget;
contentsArticles[i].onclick = addNormalClass;
}
}

window.onload = buildContentsArray;

This is the .highlighted class in an external css file:

.highlighted {
background-color: #CCFFFF;
padding: 0px 20px 0px 20px;
margin: 5px 5px 10px 5px;
font-family: Arial, Helvetica, sans-serif;
color: #BCBCBC;
}

whoisgregg

2:48 pm on Aug 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, rutabaga!

I don't see anything there that looks IE proprietary. However, there is a missing semicolon at the end of this line:

var contentsArticles = document.getElementById('maincontent').getElementsByTagName('p') 

I wonder if IE is just being more lenient with that line? Do you have Firebug installed in Firefox? If so, perhaps you could post any error messages that crop up for us to take a look at. :)

rutabaga

8:37 pm on Aug 24, 2009 (gmt 0)

10+ Year Member



Thank you for checking my code. I placed the semi-colon where you advised, but it still does not work in Mozilla; still works in IE.

Yes, I do have Firebug & have been using it, but I cannot understand why it isn't capturing the .highlighted css code as is IE. This is the error message I get in Firebug
(I don't know what \r\n means, do you?):

currentTarget is null

highlightTarget()maincontent.js (line 22)
currentTarget.className = 'highlighted';\r\n

In Microsoft Script Editor it expresses the value of .classname which is highlighted; whereas I can't find an equivalent in Firebug for .classname.

Hope this clarifies it—and hope you can understand why Mozilla won't highlight the paragraph. Thanks again.

whoisgregg

1:19 am on Aug 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahh, I see it now. :) Element IDs cannot start with a number, they must start with a letter. You can still use your general approach, you just have to modify how the IDs are assigned and then you have to modify how you access them.

Add a single letter before the numbers:

<div id="a5"></div>

Then in your code, change this line:

var currentTarget = document.getElementById(elementId);

to:

var currentTarget = document.getElementById('a'+elementId);

Should fix it up. :)

rutabaga

8:49 pm on Aug 25, 2009 (gmt 0)

10+ Year Member



I'm new to jscript so I hope I don't sound too dense; but I'm not following your advice "to add a single letter before numbers in my divs." Where is there a number in my divs? (The linked hrefs appear to require the # sign.) I understand what you are saying, but I can't understand where in my code it is that you are referring. Could you point to it? I hope I don't sound like a total dummy. Thanks so much.