开发者

Getting the title of a link, AJAX

I'm new to Javascript, and I'm sort of stuck on this.

I have a XMLHttpRequest objec开发者_StackOverflowt which is connected to some link, and I can get the responseXML from it. Now, how would I get the value of the <title> tag, from the responseXML?

I found a way to do it with a regex, but, its very ugly, and I would rather not use it (it uses responseText).

I also cannot use jQuery, Prototype, etc.


If you have an XML response the property xhr.responseXML holds the XML document, so use the DOM API

var titleElement = xhr.responseXML.getElementsByTagName('title')[0];
var titleText = titleElement.textContent

In order to make it more cross-browser use

var titleElement = xhr.responseXML.getElementsByTagName('title')[0];
var titleText = titleElement.textContent // DOM Level 3 compatible browsers
    || titleElement.innerText // IE
    || (titleElement.firstChild && titleElement.firstChild.nodeValue) // other
    || '';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜