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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlighting Bookmarked Areas 1

Status
Not open for further replies.

rutabaga

Technical User
May 28, 2009
6
US
I've been trying to adapt the below code to my own code but it didn't work. So, finally I tried to run this code as is & it didn't work either.

I'm trying to highlight bookmarked areas with a hyperlinked list. Clicking on a link is supposed to highlight the bookmarked paragraph; my understanding was that once you clicked a different link that particular highlighted paragraph would change back to normal & the newly clicked linked paragraph would be highlighted. However,in FF2 & IE6 & 8, nothing occurs.

I am farirly new to Javascript, but ususally if I get a script, I can adapt it to my needs. Here is the code:

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

<script type="text/javascript"> 
function addNormalClass() { 
this.className = 'normal'; 
} 
function highlightTarget() { 
// Collect the relevant items: 
var contentsArticles = document.getElementById('articles').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 = 'normal'; 
} 
} 
// 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('contents').getElementsByTagName('a'); 
var contentsArticles = document.getElementById('articles').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; 
</script> 
<style type="text/css"> 
.highlighted { background-color:#f90; } 
.normal { background-color:transparent; } 
</style> 
</head> 
<body> 
<ul id="contents"> 
<li><a href="#lorem">Lorem</a></li> 
<li><a href="#ipsum">Ipsum</a></li> 
<li><a href="#dolor">Dolor</a></li> 
<li><a href="#sit">Sit</a></li> 
</ul> 
<div id="articles"> 
<p id="lorem">Phasellus quis ligula eu lacus congue vulputate.</p> 
<p id="ipsum">In hac habitasse platea dictumst. Aenean tempor mattis arcu.</p> 
<p id="dolor">Maecenas sit amet turpis. Vivamus ac justo. Fusce in tortor.</p> 
<p id="sit">Duis ornare urna et velit. Phasellus vulputate turpis vitae quam.</p> 
</div> 
</body> 
</html>
[code]
 
Your code, modified only to put the missing <html> tag at the top, works fine for me in IE6 and Fx3 with no errors at all.

Perhaps if you add the missing <html> tag it might work for you in IE6 too?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Hmm.. then again, I just tried removing the <html> tag and it still worked in IE6. Are you running this over the file:// protocol or http:// ? I tried the former, so perhaps you are using the latter?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
I have been using the preview button in MS Expression Web 1, using IE6 & FF2. I've used IE8 through another computer using which you can try as well.

Do you see any highlighting (background color orange-yellow)? It moves to the correct bookmark, but no highlighting, much less un-highlighting when I try it.

Thanks.
 
I've tried it now in IE6, IE7, IE8, Fx2, and Fx3 - and it works as I think you intend (click a link, relevant para is selected with an orange BG. Click another link, new para is selected, old one is unselected).

If that's not the desired behaviour, then I've got it all wrong.

If that is the desired behaviour, then your code is working fine, so it's probably some configuration issue with your browsers or some third-party internet or security software (e.g. Norton, Symantec, etc) blocking your script from running.

If you copy the exact code you posted above, add the open <html> tag at the top, save if on your HDD as something like "C:\linktest.html", then open "C:\linktest.html" in IE (and allowing any scripts to run if you see a warning), does it not work for you? Do you see any errors or a yellow triangle in the left of the browser status bar?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Aah - I've just tried the link you've posted, and it is very much NOT the same as the code you posted above, is it?

For starters, you've introduced a basic syntax error, with a missing open brace after your "highlightTarget" function declaration.

Have you been testing using the code you posted or this other code you've broken?



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Thank you so much BRPS! That darn opening brace did it. It was in my Eweb and that's the one I uploaded to my web testing site. It's amazing how you can miss these. Now I need to adapt this to the code I'm using for the remake of my regular website. [2thumbsup]
 
It's amazing how you can miss these.

It is, given that both Firefox and IE show a syntax error on that line or the line following it...

But it's good that it's working for you now anyway.

Dan





Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Quote: "It is, given that both Firefox and IE show a syntax error on that line or the line following it..."

Hoping that I don't sound too dumb, I must say that I did not see where IE or FF showed any syntax errors. And not even in my EWeb designer program. I need to eyeball each Javascript line myself.

Still working on the adaptation...thanks.
 
Fx shows a syntax error on line following the error in its JS console (Tools -> Error console), while IE (depending on how you have it configured) will either pop up an alert saying 'Expected { at line 10' or put a small yellow triangle in the left of the status bar which will show the error when clicked.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
I went to the only place I could find regarding highlighting bookmarked areas that are hyperlinked. Here is the original javascript code: Below is how I added that code to my code with my changes. It now works in IE6, except it doesn't unhighlight, and does not work in FF2 at all. My test site is: Hope you can help. Thanks.

<code>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>
<link rel="stylesheet" type="text/css" href="bookstore.css" />


<script language="javascript" type="text javascript">
<!—-
function new_win()
{
window.open("buynow1.html","winname","height=206,width=243,left=300,top=300,xscreen=300,yscreen=300");
}
// end -->

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

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 = 'normal';
}
}

// 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.onclick = highlightTarget;
contentsArticles.onclick = addNormalClass;
}
}

window.onload = buildContentsArray;

</script>

<style type="text/css">
.highlighted { background-color:#008000; }
.normal { background-color:transparent; }
</style>

</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>
<li><a href="#Breakfast_at_the_Nut_Tree">Breakfast at the Nut Tree</a></li>
<li><a href="#Party_Time">Party Time in Old Greece</a></li>
<li><a href="#Dog_Days_and_Old_Bones">Dog Days and Old Bones</a></li>
<li><a href="#Day_in_the_Life_of_Moonbeam">Day in the Life of Moonbeam</a></li>
<li><a href="#Apples_and_Oranges">Apples and Oranges</a></li>
<li><a href="#First_Cutting">First Cutting</a></li>
<li><a href="#A_Song_For_Sarah_Lee">A Song for Sarah Lee</a></li>
<li><a href="#From_Salon_to_Salvation">From Salon to Salvation</a></li>
<li><a href="#Saving_Harry_Bowles">Saving Harry Bowles</a></li>

</ul>
</div>
</div>
</div>

<!--This is just an example of the "maincontent"; The Word & What Goes Around-->

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

<p class="content"><em><strong><a name="The_Word">The Word</a>:&nbsp; </strong></em>
&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&#39;s my older brother--used to say it all the time, so
he had the cleanest teeth among any of us kids.&quot;</p>

<p class="content"><strong><em><a name="What_Goes_Around">What Goes Around:</a>&nbsp; </em></strong>
&quot;Although Archibald Diggs departed the company of his bar pals in a well
developed state of inebriation, 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&#39;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>&quot;</p>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top