HighDots Forums  

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

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Get all href values in <ul> group and compare with current url in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jerome Bei
 
Posts: n/a

Default Re: Get all href values in <ul> group and compare with current url - 05-03-2005 , 06:12 AM






Try this:

<script>
uls = document.getElementsByTagName("UL");
for (var u=0; u<uls.length; u++) {
alert("entering list ["+u+"]");
lis=uls[u].getElementsByTagName("LI");
for (var i=0; i<lis.length; i++) {
if (lis[i].childNodes[0] && lis[i].childNodes[0].href) {
alert("found link ["+lis[i].childNodes[0].href+"]");
}
}
}
</script>

(Testes on IE 6.0 only)

--Jerome

Reply With Quote
  #2  
Old   
RobG
 
Posts: n/a

Default Re: Get all href values in <ul> group and compare with current url - 05-04-2005 , 03:35 AM






michael wrote:
Quote:
Jerome Bei wrote:


Try this:

script
uls = document.getElementsByTagName("UL");
for (var u=0; u<uls.length; u++) {
alert("entering list ["+u+"]");
lis=uls[u].getElementsByTagName("LI");
for (var i=0; i<lis.length; i++) {
if (lis[i].childNodes[0] && lis[i].childNodes[0].href) {
If anything is inserted between the <li> and <a> tags, even a space,
this will fail as the <a> element will no longer be the first child
of the <li>.

Probably better to use a recursive function to go down the tree from
the <li> to see if it has an <a> element as a descendant.

Quote:
alert("found link ["+lis[i].childNodes[0].href+"]");
}
}
}
/script

(Testes on IE 6.0 only)

--Jerome


Works nicely, fetches the links if there are, and it works on Mozilla too.
But will fall over if you insert anything between the <li> and <a>
tags, as that will insert another element and the parent/child
relationship is broken. Below is a script that runs down the tree
to the first <a> and returns that (if there is one).


<script type="text/javascript">
function getULs() {
var h, i, u, lena, lenb, lis, uls;
uls = document.getElementsByTagName("UL");
for ( u=0, lena=uls.length; u<lena; u++) {
alert("entering list [" + u + "]");
lis = uls[u].getElementsByTagName("LI");
for (i=0, lenb=lis.length; i<lenb; i++) {
if ( (h = hasAchild(lis[i])) ) {
alert("found link [" + h.id + "]");
}
}
}
}

// Given an element ref, descend element tree until an <a> is found
// Return a reference to the element or null if not found
function hasAchild(x){
isA = false; // global
if (x.nodeName == 'A') {
isA = x;
return isA; // Stop descent on first A element
}
for (var i=0; i<x.childNodes.length; i++) {
// Descend if not found an A yet
if (!isA) hasAchild(x.childNodes[i]);
}
return isA;
}

</script>
</head>
<body id="theBody">
<ul>
<li><span>spacer</span> <a id="a0" href="blah" onclick="getULs();
return false;">the ref</a></li>
<li><a id="a1" href="blah" onclick="getULs(); return false;">the
ref</a></li>
<li>no ref</li>
</ul>


--
Rob


Reply With Quote
  #3  
Old   
RobG
 
Posts: n/a

Default Re: Get all href values in <ul> group and compare with current url - 05-04-2005 , 05:35 AM



RobG wrote:
Quote:
michael wrote:
[...]

// Given an element ref, descend element tree until an <a> is found
// Return a reference to the element or null if not found
function hasAchild(x){
isA = false; // global
if (x.nodeName == 'A') {
isA = x;
return isA; // Stop descent on first A element
}
for (var i=0; i<x.childNodes.length; i++) {
// Descend if not found an A yet
if (!isA) hasAchild(x.childNodes[i]);
}
return isA;
}
Had a bit of fun with this, here's a neater version:

function hasAchild(x){
isA = ( 'A' == x.nodeName)? x : false;
for (var i=0, j=x.childNodes.length; !isA && i<j; i++) {
hasAchild(x.childNodes[i]);
}
return isA;
}


--
Rob


Reply With Quote
  #4  
Old   
Michael Winter
 
Posts: n/a

Default Re: Get all href values in <ul> group and compare with current url - 05-04-2005 , 06:26 AM



On 04/05/2005 08:35, RobG wrote:

[snip]

Quote:
If anything is inserted between the <li> and <a> tags, even a space,
this will fail as the <a> element will no longer be the first child
of the <li>.

Probably better to use a recursive function to go down the tree from
the <li> to see if it has an <a> element as a descendant.
Probably better to use getElementsByTagName to find any A elements
within a list, directly. An A element would only be valid within a list
item, so it seems rather pointless to spend time walking through the tree.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.


Reply With Quote
  #5  
Old   
RobG
 
Posts: n/a

Default Re: Get all href values in <ul> group and compare with current url - 05-04-2005 , 07:27 PM



Michael Winter wrote:
Quote:
On 04/05/2005 08:35, RobG wrote:

[snip]

If anything is inserted between the <li> and <a> tags, even a space,
this will fail as the <a> element will no longer be the first child
of the <li>.

Probably better to use a recursive function to go down the tree from
the <li> to see if it has an <a> element as a descendant.


Probably better to use getElementsByTagName to find any A elements
within a list, directly. An A element would only be valid within a list
item, so it seems rather pointless to spend time walking through the tree.
Killjoy.


--
Rob


Reply With Quote
  #6  
Old   
RobG
 
Posts: n/a

Default Re: Get all href values in <ul> group and compare with current url - 05-04-2005 , 10:50 PM



RobG wrote:
Quote:
michael wrote:

[...]

Any ideas, and especially short code would be greatly appreciated!

[...]

Quote:
var f = document.URL.replace(a,''); // filename of current page
Sorry, forgot IE doesn't do document.URI, try this:

var f = document.location.href.replace(a,'');

[...]

--
Rob


Reply With Quote
  #7  
Old   
RobG
 
Posts: n/a

Default Re: Get all href values in <ul> group and compare with current url - 05-05-2005 , 05:41 AM



michael wrote:
Quote:
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


Reply With Quote
  #8  
Old   
Michael Winter
 
Posts: n/a

Default Re: Get all href values in <ul> group and compare with current url - 05-05-2005 , 06:38 AM



On 05/05/2005 03:50, RobG wrote:

[snip]

Quote:
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.


Reply With Quote
  #9  
Old   
Michael Winter
 
Posts: n/a

Default Re: Get all href values in <ul> group and compare with current url - 05-05-2005 , 06:45 AM



On 05/05/2005 06:42, michael wrote:

[snip]

Quote:
I get a javascript error:
[snip]

Quote:
if ( \bhead\b.test(x.className) ){
Where have the forward slashes gone?

if(/\bhead\b/.test(x.className)) {

Quote:
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.

Quote:
--
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.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.