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
 
for(x=0; x<document.anchors.length; x++){
a = document.anchors[x]
if(Netscape){
text = a.text
}
else{ //IE
text = a.innerText
}
if ((text == null) || (text == &quot;&quot;)) text = a.name
}

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
 
hi Jonathan,

so pdf_link is an anchor object? what exactly do you mean by comparing it to a string?

also a note about tek-tips: if you use an array index &quot;i&quot;, and leave &quot;Process TGML&quot; checked under the reply textarea, tek-tips will interpret
Code:
[i]
as &quot;print everything that follows in italics&quot; and your post will be turned into italics as you can see above. choose a different array index var or uncheck the Process TGML box.

=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
Jeff,

First off, that explains the italics.

Ok, so here is why I need to do what I need to do.
I have different webpages from different groups, and the setup is different. They don't want to change there links or names, or layouts, and I don't want to change my program. So, the easiest way is to do this is to search through names and or ids of the links. Therefore it is all hidden and everybody is happy. I need to be able to set pdf_link to what is inside <a name=&quot;...&quot;> or <id=&quot;...&quot;>. Then I need to be able to compare it like before, with the anchor name instead of the innerHTML.

What would be the best way to do this?

Jonathan
 
if (pdf_link.name) {
var sName = pdf_link.name;
}
else if (pdf_link.id) {
var sId = pdf_link.id;

}


=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
Jeff,

Do I use pdf_link = first_window.document.anchors[x]; or
pdf_link = first_window.document.links[x];

with your code.

Jonathan
 
Ok,
Here is what I put in, and it didn't work.

<code>
while (i < first_window.document.links.length)
{
var pdf_link = &quot;&quot;;
pdf_link = first_window.document.links;
var pdf_link_text = &quot;&quot;;
pdf_link_text = first_window.document.anchors;
if (pdf_link_text.name)
{
var named = pdf_link_text.name;
}
else if (pdf_link.id)
{
var named = pdf_link_text.id;
}

compare(pdf_link,named,find);
i++;
}
<code>

Where did I go wrong here. It still sees it as null. Therefore, it can't compare it.

Jonathan
 
it depends on what you're trying to get. if you want the name or id of an anchor, use document.anchors; if you want the text in a link, use document.links

=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
Jeff,

Ok, so I am using document.anchors. Keep in mind, the code above has [j] after the anchors and links.

What may be my problem then?

The error is: Name is null or not an object.

-Jonathan
 
I threw in 2 alerts:

alert(&quot;pdf_link_text = &quot; + pdf_link_text);
alert(&quot;pdf_link_text.name = &quot; + pdf_link_text.name);

The first one came back with undefined.
The second one crashed explorer. The problem is in the .name.

Jonathan
 
sounds to me like you need a tutorial in extracting properties from objects - take a look at this:

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
<title>test</title>
<script type=&quot;text/javascript&quot;>
function foo() {
var oAnc = document.anchors;
var sText = &quot;&quot;;

for (var x = 0; x < oAnc.length; x++) {
if (oAnc[x].id) {
sText += &quot;oAnc[&quot; + x + &quot;].id == \&quot;&quot; + oAnc[x].id + &quot;\&quot;\n&quot;;
}
if (oAnc[x].name) {
sText += &quot;oAnc[&quot; + x + &quot;].name == \&quot;&quot; + oAnc[x].name + &quot;\&quot;\n&quot;;
}
}

alert(sText);
}
</script>
</head>

<body>
<input type=&quot;button&quot; value=&quot;foo();&quot; onclick=&quot;foo();&quot; />
<p/>
<a name=&quot;a_1&quot;>anchor1</a>
<p/>
<a id=&quot;a_2&quot;>anchor2</a>
<p/>
<a name=&quot;a_3_name&quot; id=&quot;a_3_id&quot;>anchor3</a>
</body>
</html>




=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
Jeff,

Let me get this straight. All I have to do is take the system array and copy it to one of my own.

If that is the case, then this is great and should solve my problem.

Jonathan
 
Ok, I apparently still don't get it because I still can't get it to work.

Here is my code

Code:
 while (i < first_window.document.links.length)
            {  
               var link_list = first_window.document.anchors;
	       var pdf_link = &quot;&quot;;
               pdf_link = first_window.document.links[i];
               var pdf_link_text = &quot;&quot;;
               if (link_list[i].id) 
		{
                    pdf_link_text = link_list[i].id;
                }
               if (link_list[i].name) 
		{
                   pdf_link_text = link_list[i].name;   
                }
	       
               alert(&quot;pdf_link_text = &quot; + pdf_link_text);
               compare(pdf_link,pdf_link_text,find);
	       i++;
            }

[code]

and here is the web page I am looking at:

[code]

<A HREF=&quot;csscdraw/cssc-0002.pdf&quot; target=&quot;_top&quot;>CSSC-0002    CSSC ASSEMBLY[COLOR=red]<NAME=&quot;CSSC-0002 CSSC ASSEMBLY&quot;>[/color red]</A>

<A HREF=&quot;csscdraw/cssc-0003.pdf&quot; target=&quot;_top&quot;>CSSC-0003    ETHERNET CONNECTION<NAME=&quot;CSSC-0003 ETHERNET CONNECTION&quot;></A>

<A HREF=&quot;csscdraw/cssc-0004.pdf&quot; target=&quot;_top&quot;>CSSC-0004    DEC SERVER ASSEMBLY<NAME=&quot;CSSC-0004 DEC SERVER ASSEMBLY&quot;></A>

<A HREF=&quot;csscdraw/cssc-05.pdf&quot; target=&quot;_top&quot;>CSSC-05    RS422 TO RS232 CONVERTER ENCLOSURE<NAME=&quot;CSSC-05 RS422 TO RS232 CONVERTER ENCLOSURE&quot;></A>
 
you don't have any anchors on your page! an anchor is an <a> tag with an id or name attribute. a link is an <a> tag with an href attribute.

=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
Ok, I kind of wondered about that. Is there a way to name a link?

Jonathan
 
to view all <a> tags by their ID use this...

<script>
function anchors(){
tags = document.getElementsByTagName(&quot;a&quot;)
for(x=0; x<tags.length; x++){
if (tags[x].href != &quot;&quot;){
//you've found a link
}
}
</script>



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 guess my question is this. Is it possible to place id in the same set as a href. Such as this:

<a href=&quot;...&quot;>...<id=&quot;...&quot;></a>

and then do something like this:

var pdf_link = first_window.document.links[j];
var pdf_link_text = first_window.document.links[j].id;
or
var pdf_link_text = first_window.document.links[j].name;

What I need to do inthe end is be able to compare the id or name with a given input. And if it matches, then copy the id or name and the address to arrays.

- Jonathan
 
BTW - you can name all of your links if you'd like
<a href=&quot;nextPage.htm&quot; name=&quot;nextPage&quot;>Next Page</a>

run this...

<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;\n&quot;
}
}
alert(msg)
}
</script>
<body onLoad=&quot;anchors()&quot;>
<a href=&quot;<a href=&quot;<a href=&quot;<a name=&quot;local&quot;>local</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
 
Wolf,

Here is my problem.

I don't need the address like it is given by your code. I need to be able to compare the id of the link.

For instance.
Here is the javascript code:

pdf_link_text = first_window.document.anchors[j];

and here is the html link it is looking at:

<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>

When I run the program, pdf_link_text becomes csscdraw/cssc-5-1.pdf, and I want it to become CSSC1.

How do I do that?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top