PDA

View Full Version : <a> attributes regexp



gilgalbiblewhee
01-23-2011, 05:52 AM
how about getting the attributes of the a tag? href and title and the innerhtml (actually the innerhtml has space on both ends)

venegal
01-23-2011, 05:59 AM
Why parse the link yourself when your browser has a perfectly good HTML parser built in? Just use the DOM.

gilgalbiblewhee
01-23-2011, 06:38 AM
Why parse the link yourself when your browser has a perfectly good HTML parser built in? Just use the DOM.
I don't get it

venegal
01-23-2011, 07:13 AM
Ok; I don't know what you know and what you don't so I'll just try to keep this simple:

When a browser reads in the HTML source of a page, it parses all the tags into a tree structure (the DOM), which can then easily be manipulated by Javascript. Tags become nodes in that tree, attributes become properties of the nodes.

What you want to do, is access the attributes of a tag, which you can easily do by accessing the properties of the node there's no need to parse the tag again; your browser has already done that for you.

Example:

var a = document.getElementsByTagName('a')[0];
alert(a.href);
alert(a.title);
alert(a.innerHTML);

That is, assuming that the link you're talking about is actually part of your page.

If it isn't, and you just have the HTML source for that link as plain text, you don't have to parse it yourself, either. You can just let the browser parse it into the DOM and then access it like you would have before.

Example:

var div = document.createElement('div');
div.innerHTML = '<a href="http://google.com/" title="Google">Link to Google</a>';
var a = div.getElementsByTagName('a')[0];
alert(a.href);
alert(a.title);
alert(a.innerHTML);

gilgalbiblewhee
01-23-2011, 09:26 AM
Ok; I don't know what you know and what you don't so I'll just try to keep this simple:

When a browser reads in the HTML source of a page, it parses all the tags into a tree structure (the DOM), which can then easily be manipulated by Javascript. Tags become nodes in that tree, attributes become properties of the nodes.

What you want to do, is access the attributes of a tag, which you can easily do by accessing the properties of the node there's no need to parse the tag again; your browser has already done that for you.

Example:

var a = document.getElementsByTagName('a')[0];
alert(a.href);
alert(a.title);
alert(a.innerHTML);

That is, assuming that the link you're talking about is actually part of your page.

If it isn't, and you just have the HTML source for that link as plain text, you don't have to parse it yourself, either. You can just let the browser parse it into the DOM and then access it like you would have before.

Example:

var div = document.createElement('div');
div.innerHTML = '<a href="http://google.com/" title="Google">Link to Google</a>';
var a = div.getElementsByTagName('a')[0];
alert(a.href);
alert(a.title);
alert(a.innerHTML);
Oh I see thanks.