Click to See Complete Forum and Search --> : Getting an elements attribute value


Vattic
05-15-2008, 07:44 PM
I have an iframe and I am using the following to find the iframe:

var allTags, thisTag;
allTags = document.evaluate(
"//iframe[@ALLOWTRANSPARENCY='true']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);

As the page only ever has one iframe I don't know if this is the best way to do that but it works for now.

The problem is I want to have the iframes "scr" attribute value as a string.

Is this possible, and how?

Cheers.

Kor
05-16-2008, 03:47 AM
This is XPath, not Javascript... Probably I should move this thread to the XML Forum on which could be bore related. Anyway, even if I am not too familiar with XPath, an attribute is to be selected using the expression @, thus @src should select all the attributes src of the element. Something like:

/iframe/@src

rpgfan3233
05-16-2008, 11:24 AM
I think this would be faster using DOM scripting than XPath:
var example = document.getElementsByTagName('iframe')[0].src;

If you had multiple iframes, I wouldn't hesitate to use a for...in loop to loop through each iframe and grab the src attribute:
var example = new array(); //an array to hold all of the strings used for the src attribute
for (i in document.getElementsByTagName('iframe'))
example.push(i.src);

Simple enough, right?

Edit: If you really need to use XPath, the expression would be this, I believe:
//iframe[@ALLOWTRANSPARENCY='true']/@src