document.getElementsByTagName??

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

document.getElementsByTagName??

Post by d3ad1ysp0rk »

I've been looking around, and I found this code, that if you enter this into a browser window, it will get the contents of a hyperlink (the part in bold):

Code: Select all

<a href="http://www.google.com/">&#1111;b]Google.com&#1111;/b]</a>
but im not so sure how to get the attribute for href (ie. getting "http://www.google.com/" instead of "Google.com"..

Code: Select all

javascript:w=window.open("");
a=document.getElementsByTagName("a");
for(c=32;c<98;c++)
for(i=0;i<3;i++)
w.document.write("<iframe src='http://www.site.com/page.php?name="+a&#1111;c].innerHTML+"' width='100' height='100'></iframe>");
void(0);
(its all one line, so you can paste it into your browser, but i added line breaks so u can read it..)


any ideas or functions that would do this?

thanks
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

The toString() method of the A class returns the href attribute of the object. Thus either you do:

Code: Select all

w.document.write("<iframe src='http://www.site.com/page.php?name="+a[c].href+"' width='100' height='100'></iframe>");
or just:

Code: Select all

w.document.write("<iframe src='http://www.site.com/page.php?name="+a[c]+"' width='100' height='100'></iframe>");
These are not PHP, just using syntax-highlighting ;)
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Don't forget that getElementsByTagName() will only work on browsers that are DHTML compatible... ie IE5.5+ and NS6+ (not too sure about other browsers) so it might be worth having some kind of browser detection on your site. :)


Come to think of it you could find out simply by using

Code: Select all

var isDHTML = document.getElementById != -1 ? true : false
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Thanks a lot you guys :)
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

I was working on getting the <a> tags of all urls ending with bz2.
var ela=document.getElementsByTagName("A");
for (i=0;i<ela.length;i++)
{
if (ela.item(i).href.substr(-3,3)=="bz2")
txtLinks+=ela.item(i).Name+" : "+ela.item(i).href+'<BR>';
}
But name is undefined, How do I get the name (caption or whatever) ?

<A HREF="url.php">ThisName</A>
tagName will return A
href will return url.php
what will return ThisName ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't remember a Name property.. maybe a name property...

you can try .innerHTML too.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Yep, innerHTML will do the trick. Just replace "ela.item(i).Name" with "ela.item(i).innerHTML" and it should chuck out what you need it to ;)
Post Reply