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

anchor array help 2

Status
Not open for further replies.

skills

Programmer
Jun 24, 2003
60
US
I am trying to access the array of anchors. What I want to do is take either the name or id and assign it to a variable. This is used later on in an internal search engine. I am having a problem with null.

What I have now is this:

pdf_link = first_window.document.anchors;

Is it possible to compare pdf_link to a string of text?

Regard,

Jonathan
 
tags[x].name
tags[x].id

will give you a name or id for each link (it will be blank if you haven't named the link or given it an id)

<a href=&quot;somePage.htm&quot; name=&quot;linkName&quot; id=&quot;linkID&quot;>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
ok, I don't get it.

It still doesn't like it.

I get a &quot;id&quot; is null or not an object. Same with the name.

Here is my code:

Code:
while (j < first_window.document.links.length)
            {  
               var pdf_link = &quot;&quot;;
               pdf_link = first_window.document.links[j];
               var pdf_link_text = &quot;&quot;;
               var tags = document.getElementsByTagName(&quot;a&quot;)
	       if (tags[j].name != null)
                  {
                     pdf_link_text = tags[j].name;
                  }
	       if (tags[j].id != null)
                  {
                     pdf_link_text = tags[j].id;
                  }
               alert(pdf_link_text);

What is wrong with this code?

- Jonathan
 
pretty much everything is wrong with that code.

if you're saying that you want to extract the &quot;id&quot; from an <a> tag, here is how to do it:

var a = document.getElementsByTagName(&quot;a&quot;);
var sId = &quot;&quot;;

for (var x = 0; x < a.length; x++) {
if (a[x].id) {
sId = a[x].id;
}
else {
sId = &quot;no id value!&quot;
}
alert(sId);
}


=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
That's it. It FINALLY works! I appreciate all your help with this.

Jonathan
 
Your code isn't even attempting my last few posts...

Run this code...

<script>
function anchors(){
tags = document.getElementsByTagName(&quot;a&quot;)
msg = &quot;&quot;
for(x=0; x<tags.length; x++){
if (tags[x].href != &quot;&quot;){
msg += tags[x].href + &quot; - &quot; + tags[x].name + &quot; - &quot; + tags[x].id + &quot;\n&quot;
}
}
alert(msg)
}
</script>
<body onLoad=&quot;anchors()&quot;>
<a href=&quot; name=&quot;googleLink&quot; id=&quot;googleID&quot;>Google</a><br>
<a href=&quot; name=&quot;espnLink&quot;>ESPN</a><br>
<a href=&quot; ID=&quot;msnID&quot;>MSN</a><br>
<a name=&quot;local&quot;>local</a>
<A HREF=&quot;csscdraw/cssc-5-1.pdf&quot; id=&quot;CSSC1&quot; target=&quot;_top&quot;>CSSC-5-1 CSSC SYSTEM PIP/SPC WIRING PLAN</A>


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
I have a slight problem with my coding.

It searches great, except that on a few pages, there is this:

<A NAME=&quot;2002&quot;>
<FONT SIZE=&quot;+3&quot;><CENTER>2002</CENTER></FONT>
<HR SIZE=2 color=&quot;black&quot;>
<BR>


<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=4 WIDTH=99%>
<TR><TD>2002_020</TD><TD><A HREF=&quot;fis\fis-02-020.pdf&quot; ID=&quot;...&quot;>...</A></TD></TR>
<TR><TD>2002_019</TD><TD><A HREF=&quot;fis\fis-02-019.pdf&quot; ID=&quot;...&quot;>...</A></TD></TR>

.

Here is the coding from before

var a = first_window.document.getElementsByTagName(&quot;a&quot;);
if (a[j].id)
{
pdf_link_text = a[j].id;
}
else
{
pdf_link_text = &quot;no id value!&quot;;
}

My problem is that the (a name) is throwing off the link by one, or two, or how many ever (a name) there are.

What do need to have it skip over the (a name)s.

Skills
 
Look in my code again - you need something like this line...

if (tags[x].href != &quot;&quot;){


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
I am not sure how that would fit into my code.

Here is what I have now:

Note this works except for those rare occasions I mentioned above.


var a = first_window.document.getElementsByTagName(&quot;a&quot;);
if (a[j].id)
{
pdf_link_text = a[j].id;
}
else
{
pdf_link_text = &quot;no id value!&quot;;
}
if (pdf_link_text != &quot;no id value!&quot;)
{
compare(pdf_link,pdf_link_text,find);
}


Skills
 
if (a[j].id && a[j].href != &quot;&quot;)


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Ok, that didn't work so I am going to make this easy and just post the whole code. Here goes:


function search(first,find)
{
var i = 0;
var first_window = window.open(first,&quot;first_window&quot;,&quot;width=1,height=1,top=1700,left=1700&quot;);
while (first_window.document.readyState != &quot;complete&quot;)
{
var waiting = 0;
}
while (k < first_window.document.links.length)
{
var pdf_link = &quot;&quot;;
pdf_link = first_window.document.links[k];
var pdf_link_text = &quot;&quot;;
var a = first_window.document.getElementsByTagName(&quot;a&quot;);

if ((a[k].id) && (a[k].href != &quot;&quot;))

{
pdf_link_text = a[k].id;
}
else
{
pdf_link_text = &quot;no id value!&quot;;
}
if (pdf_link_text != &quot;no id value!&quot;)
{
compare(pdf_link,pdf_link_text,find);
}

k++;
}
first_window.close();
}




<BR><BR><HTML>
<a name=&quot;top&quot;>
<HEAD>
<TITLE>Drawings</TITLE>

</HEAD>
<style>
<!--
a:hover{color:red}
a{text-decoration:none}
//-->
</style>
<BODY TEXT=&quot;#000000&quot; link=&quot;black&quot; vlink=&quot;black&quot;>
<A NAME=&quot;2003&quot;>
<FONT SIZE=&quot;+3&quot;><CENTER>2003</CENTER></FONT>
<HR SIZE=2 color=&quot;black&quot;>
<BR>


<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=4 WIDTH=99%>

<TR><TD>2003-001</TD><TD><A HREF=&quot;...&quot; ID=&quot;...&quot;>...</A></TD></TR>
<TR><TD>2003-002/TD><TD><A HREF=&quot;...&quot; ID=&quot;...&quot;>...</A></TD></TR>
</TABLE><BR>
<TR><TD>2003-003/TD><TD><A HREF=&quot;...&quot; ID=&quot;...&quot;>...</A></TD></TR>
<TR><TD>2003-004/TD><TD><A HREF=&quot;...&quot; ID=&quot;...&quot;>...</A></TD></TR>
</TABLE><BR>
<TR><TD>2003-005/TD><TD><A HREF=&quot;...&quot; ID=&quot;...&quot;>...</A></TD></TR>
<TR><TD>2003-006/TD><TD><A HREF=&quot;...&quot; ID=&quot;...&quot;>...</A></TD></TR>
</TABLE><BR>

<HR SIZE=2 color=&quot;black&quot;>
<BR><BR><BR><BR><BR><BR>

<A NAME=&quot;2002&quot;>[/blue]
<FONT SIZE=&quot;+3&quot;><CENTER>2002</CENTER></FONT>
<HR SIZE=2 color=&quot;black&quot;>
<BR>

<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=4 WIDTH=99%>

<TR><TD>2002-001</TD><TD><A HREF=&quot;...&quot; ID=&quot;...&quot;>...</A></TD></TR>
<TR><TD>2002-002</TD><TD><A HREF=&quot;...&quot; ID=&quot;...&quot;>...</A></TD></TR>
<TR><TD>2003-003/TD><TD><A HREF=&quot;...&quot; ID=&quot;...&quot;>...</A></TD></TR>
<TR><TD>2003-004/TD><TD><A HREF=&quot;...&quot; ID=&quot;...&quot;>...</A></TD></TR>
</TABLE><BR>


The code in green is the search part. The code in red is the file being searched. Somehow, pdf_link_text is being linked with the address of 2 down. The items in blue are throwing off the links.


Hopefully this may help.

Skills
 
Run this - it's working for me....

<script>
function showLinks(){
a = document.getElementsByTagName(&quot;a&quot;)
var msg = &quot;&quot;
for (x=0; x<a.length; x++){
if (a[x].href != &quot;&quot; && a[x].id != &quot;&quot;){
msg += &quot;\n&quot; + x + &quot; - HREF:&quot; + a[x].href + &quot; ---- ID:&quot; + a[x].id + &quot;>>>>> Name:&quot; + a[x].name
}
else{
msg += &quot;\n---not a link&quot; + x + &quot; - HREF:&quot; + a[x].href + &quot; ---- ID:&quot; + a[x].id + &quot;>>>>> Name:&quot; + a[x].name
}
}
alert(msg)
}
</script>



<BR><BR><HTML>
<a name=&quot;top&quot;>
<HEAD>
<TITLE>Drawings</TITLE>

</HEAD>
<style>
<!--
a:hover{color:red}
a{text-decoration:none}
//-->
</style>
<BODY TEXT=&quot;#000000&quot; link=&quot;black&quot; vlink=&quot;black&quot;>
<A NAME=&quot;2003&quot;>
<FONT SIZE=&quot;+3&quot;><CENTER>2003</CENTER></FONT>
<HR SIZE=2 color=&quot;black&quot;>
<BR>


<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=4 WIDTH=99%>

<TR><TD>2003-001</TD><TD><A HREF=&quot;http: ID=&quot;1&quot;>...</A></TD></TR>
<TR><TD>2003-002/TD><TD><A HREF=&quot;http: ID=&quot;2&quot;>...</A></TD></TR>
</TABLE><BR>
<TR><TD>2003-003/TD><TD><A HREF=&quot;http: ID=&quot;3&quot;>...</A></TD></TR>
<TR><TD>2003-004/TD><TD><A HREF=&quot;http: ID=&quot;4&quot;>...</A></TD></TR>
</TABLE><BR>
<TR><TD>2003-005/TD><TD><A HREF=&quot;http: ID=&quot;5&quot;>...</A></TD></TR>
<TR><TD>2003-006/TD><TD><A HREF=&quot;http: ID=&quot;6&quot;>...</A></TD></TR>
</TABLE><BR>

<HR SIZE=2 color=&quot;black&quot;>
<BR><BR><BR><BR><BR><BR>
<A NAME=&quot;2002&quot;>
<FONT SIZE=&quot;+3&quot;><CENTER>2002</CENTER></FONT>
<HR SIZE=2 color=&quot;black&quot;>
<BR>

<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=4 WIDTH=99%>

<TR><TD>2002-001</TD><TD><A HREF=&quot;http: ID=&quot;7&quot;>...</A></TD></TR>
<TR><TD>2002-002</TD><TD><A HREF=&quot;http: ID=&quot;8&quot;>...</A></TD></TR>
<TR><TD>2003-003/TD><TD><A HREF=&quot;http: ID=&quot;9&quot;>...</A></TD></TR>
<TR><TD>2003-004/TD><TD><A HREF=&quot;http: ID=&quot;10&quot;>...</A></TD></TR>
</TABLE><BR>
<input type=button onclick=&quot;showLinks()&quot; value=&quot;show links&quot;>


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
I tried throwing in

if (a[x].href != &quot;&quot; && a[x].id != &quot;&quot;){

into my code, and it still didn't work.

Also, what is with the

msg += &quot;\n&quot; + x + &quot; - HREF:&quot; + a[x].href + &quot; ---- ID:&quot; + a[x].id + &quot;>>>>> Name:&quot; + a[x].name

Is that just for testing purposes? Why don't you use my code?

if (a[j].href != &quot;&quot; && a[j].id != &quot;&quot;)

{
pdf_link_text = a[j].id;
}
else
{
pdf_link_text = &quot;no id value!&quot;;
}
if (pdf_link_text != &quot;no id value!&quot;)
{
compare(pdf_link,pdf_link_text,find);
}


Any more suggestions?

Skills
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top