473,773 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get all href values in <ul> group and compare with current url

Is it possible to get all href URLs contained in a unordered list and place
them in an array?
Or in fact two different arrays, differently named one for each <ul> group?

<ul>
<li><a href="lemurs.ht ml">Lemurs</a></li>
<li><a href="chameleon s.html">Chamele ons</a></li>
</ul>

<ul>
<li><a href="sharks.ht ml">Sharks</a></li>
<li><a href="crocs.htm l">Crocodile s</a></li>
</ul>

I want to fetch an array of the links within a <ul> group, and
document.write the above, but with class variable in which I can
set a different value, depending on if the current url filename matches
exactly any one of the links within the <ul> group the <li> belongs to.

More specifically, I would like to ...

document.write( '<li class'+myvariab le+'>')
|
... class 'green' or 'blue' -------'
depending on whether filename in any href contained
in an <li> of the <ul> group it is in.
If an exact match is found, write out green, if not blue.

The file name portion of the current URL I simply get through:

var f = window.location .href;
file = i.substring (f.lastIndexOf( '/') +1);

It is for drop-down nav-system whereby all list items would of a ul group
would appear either one of two colours as specified in css properties. All
links are internal links on the same domain so one would always match.

I could naturally do it all by having a pre-typed list of all filenames and
run each <li> through an if statement before writing it out. But as there
will be many <li>'s I would prefer a more typeless method if possible.

Many thanks for any tips!

Michael

--
(1) Everything depends.
(2) Nothing is always.
(3) Everything is sometimes.

Jul 23 '05
16 4452
> interested in descendant ULs, you may be) as an example, but my idea
of what you are trying to do is probably very different to what you
are actually doing.


This looks convertable to exactly what I need, perfect!

And code can't get much shorter. Only, I get a javascript error:

--
Error: illegal character
Source File: file:///tmp/z2.html
Line: 24, Column: 10
Source Code:
if ( \bhead\b.test(x .className) ){

Error: doClass is not defined
--

I don't know how to fix that, I guess second error is due to the first.

Many thanks,
Michael
--
The first duty of a revolutionary is to get away with it.
-- Abbie Hoffman

Jul 23 '05 #11
michael wrote:
interested in descendant ULs, you may be) as an example, but my idea
of what you are trying to do is probably very different to what you
are actually doing.

This looks convertable to exactly what I need, perfect!

And code can't get much shorter. Only, I get a javascript error:

--
Error: illegal character
Source File: file:///tmp/z2.html
Line: 24, Column: 10
Source Code:
if ( \bhead\b.test(x .className) ){

Error: doClass is not defined
--

I don't know how to fix that, I guess second error is due to the first.

Many thanks,
Michael
--
The first duty of a revolutionary is to get away with it.
-- Abbie Hoffman


Don't know why you get the error, I tested in IE and Firefox. Have
you created files for each link? And included the modified URL/href
line?

--
Rob
Jul 23 '05 #12
On 05/05/2005 03:50, RobG wrote:

[snip]
Sorry, forgot IE doesn't do document.URI, try this: [...]


Not URI, no, but URL, yes. Unless Microsoft are lying in their
documentation, it's been included in all versions, on all supported
platforms, since 4.0.

Have you found a version where it fails to work, or are you just
assuming it doesn't?

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #13
On 05/05/2005 06:42, michael wrote:

[snip]
I get a javascript error:
[snip]
if ( \bhead\b.test(x .className) ){
Where have the forward slashes gone?

if(/\bhead\b/.test(x.classNa me)) {
Error: doClass is not defined
--

I don't know how to fix that, I guess second error is due to the first.
Yes. The parsing error within the function causes the parser to halt the
creation of the function object. When the browser tries to call doClass
in response to the load event, it fails as the object doesn't exist.
--
The first duty of a revolutionary is to get away with it.
-- Abbie Hoffman


FYI: Your signature separator seems to be broken. It should be
dash-dash-space.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #14
> Where have the forward slashes gone?

Thanks for mentioning it, I suspect something's wrong with my Linux
font-config set-up or X-windows, or just the newsreader itself.
I just viewed source of the posting and indeed found the missing forward
slashes- I shall try again with "/" and I'm sure it will work much better!

Michael

--
"Don't worry about people stealing your ideas. If your ideas are any
good, you'll have to ram them down people's throats."
-- Howard Aiken
Jul 23 '05 #15
> <li><a href="z4.html"> Pretty&nbsp;fis h</a></li>
<li><a href="z5.html"> Don&#39t&nbsp;eat</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>

This works great!

But does anyone know how to get it working as a horizontally aligned drop
menu, only showing 'sub' list items when hovering over the first <li> of
each <ul class="head low">, so if for example hovering over Flora & Fauna
or any of its sub-links, show:

[Flora & funa] [Aquatic creatures[
[Pretty flowers]
[Deadly vines]

And retaining the background scheme for all items, but with a variable, so
the current page is not actually a link?

--
Blore's Razor:
Given a choice between two theories, take the one which is
funnier.

Jul 23 '05 #16
I pieced together a hover-drop-menu derived from
htmldog.com/articles/suckerfish/dropdowns with the colour-by-URL modifier
script solution as posted by Rob earlier.

The new styles basically removes the sub-listed items from view, leaving
only the first LI of the <ul class="head low"> visible until mouse hovers
over it, to bring its sub-points back into view.

The background colour works for the LI's on top, which are always visible,
but not for the sub-points for some reason.
Although font styles do take effect across a complete lists, for example: ..

..high {background-color: #99FFCC; font-size:80%;}

... makes a smaller font on all items in the group containing current URL.

Anyone has any idea why the background colours doesn't take effect behind
the sub-points?

See complete page below:

<html><head><ti tle></title>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<style type="text/css">

#menu, #menu ul {
padding: 0;
margin: 0;
list-style:none;
}

#menu a {
display: block:
width: 10em:
}

#menu ul {
float: left;
width: 10em;
}

#menu li ul {
position: absolute;
width: 10em;
left: -999em;
}

#menu li:hover ul {
left: auto;
}

#menu li:hover ul{
left: auto;
}
#menu li.sfhover ul{ /* ie specific, but alignment not yet working */
left: auto;
margin-top:1em;margin-left:-1em;
}
..high {background-color: #99FFCC; font-size:80%;}
..low {background-color: #33CC00;}

</style>

<script type="text/javascript">

function doClass(){
var As, ff, x;
var a = RegExp('.*/'); // Needed many times, compile for speed
var f = document.URL.re place(a,''); // filename of current page
var d = document.getEle mentById('menu' );
var uls = d.getElementsBy TagName('ul');

for (var i=0, j=uls.length; i<j; i++){
x = uls[i];

// If the UL className string includes word 'head'
if ( /\bhead\b/.test(x.classNa me) ){

// Get its A elements
As = x.getElementsBy TagName('a');

// Search for matching file names
for (var k=0, m=As.length; k<m; k++) {
ff = As[k].href.replace(a ,'');

// If match, modify the className string of the parent UL
if ( ff == f ){
x.className = x.className.rep lace(/\blow\b/,'high');

// No need to continue once we've found one
return;
}
}
}
}
}
</script>
<script language="javas cript">

/* the suckerfish script to fix IE compatibility in bringing the sub-menu
items back in view, although not as intended directly under its headings */

sfHover = function() {
var sfEls = document.getEle mentById("menu" ).getElementsBy TagName("LI");
for (var i=0; i<sfEls.length ; i++) {
sfEls[i].onmouseover=fu nction() {
this.className+ =" sfhover";
}
sfEls[i].onmouseout=fun ction() {
this.className= this.className. replace(new RegExp(" sfhover\\b"), "");
}
}
}
if (window.attachE vent) window.attachEv ent("onload", sfHover);

</script>
</head>
<body onload="doClass ();">

<div id="menu">

<ul class="head low">
<li><a href="z0.html"> Flora&nbsp;&amp ;&nbsp;puna</a>

<ul class="sub">
<li><a href="z1.html"> Pretty&nbsp;flo wers</a></li>
<li><a href="z2.html"> Deadly&nbsp;vin es</a></li>
</ul>
</li>

</ul>
<ul class="head low">
<li><a href="z3.html"> Aquatic&nbsp;cr eatures</a>

<ul class="sub">
<li><a href="z4.html"> Pretty&nbsp;fis h</a></li>
<li><a href="z5.html"> Don&#39t&nbsp;eat</a></li>
</ul>

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

</body>
</html>

--
One way to make your old car run better is to look up the price of a
new model.

Jul 23 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4662
by: bearclaws | last post by:
I am looping through a list of categories and want to display the list horizontally (instead of vertically). I want to create a single row with 4 list items in each cell of the row. I thought this would work but I get this error: "End tag 'xsl:if' does not match the start tag 'ul'." Any thoughts?
5
9245
by: toylet | last post by:
Attached is some css codes for a website. It has 3 parts: top-bar, side-bar (on the left) and main-body. The top-bar has a mouseover menu called top-menu implemented via ul:hover. When the mouse pointer hovers over the top-menu, the bottom margin of the top-bar expands downwards. How could I make the hover menu to stack on top of the main-body, not expanding the bottom margin of the top-bar? I believe it has something to do with...
19
17573
by: CMAR | last post by:
I have the following markup. The problem is that the browser, e.g., IE6, inserts several lines of blank space between the <div> and the following table. Is there a way to minimize that vertical spacing? Thanks, CMA <div class="vlgray">Condition</div> <table cellpadding="0" cellspacing="0">
4
2210
by: abs | last post by:
Anybody has an idea how to get the <ul> element which is not nested in <li> element ? In other words I have several lists like this: <ul id="1"> <li>Aaaaaaaa</li> <li>Bbbbbbbb</li> <li>Cccccccc <ul> <li>111111</li> <li>222222</li>
2
7632
by: Shaun | last post by:
Hello! I have a quick question regarding CSS and having it applied to all elements. I am trying to eliminate the gap between a paragraph and a list that usually occurs in html and I've found away to do that with this code: <p>a</p> <ul style="margin-top: -20; padding-top: 0"> <li>1</li>
11
5175
by: namenotgivenhere | last post by:
My design goal is to make the white space above and below <p> and <ul> equal to the height of my font. The first step to achieving this I believe is to have an equal amount of white space above or below <p> and <ul>. Can someone suggest a method using css to accomplish this? I have succeeded in doing this in IE6 but I'd also like to know if there's a method to achive this goal that will have a similar visual look in other modern browsers....
3
6689
by: Man-wai Chang | last post by:
A 2 columns x 10 rows matrix input form <ul> <li> <ul> <li>item name 1 <li><input type="textbox" name="input_col_1_row_1"> <li><input type="textbox" name="input_col_1_row_2"> </ul> <li>
6
4472
by: capricious | last post by:
Is it possible, so that when you do multiple <UL>'s to control how deep the UL's are marked? For example, it would defaultly look like this with multiple ULs and LIs: -- Code : Main Menu<ul>Fruits<ul><li>Apples</li><li>Oranges</li></ul></ul> .. -- Returns :
2
2362
by: Shahid | last post by:
Hi, I am parsing an .HTML file that contains following example code: <div> <p class="html_preformatted" awml:style="HTML Preformatted" dir="ltr" style="text-align:left"><span style="font-size:12pt;font- family:'Arial'" xml:lang="en-US" lang="en-US">Normal Text Arial 12 Black before bullets.</span></p> <ul> <li class="html_preformatted" dir="ltr" style="text- align:left">&nbsp;<span style="font-size:12pt;font-family:'Arial'"
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.