1

我需要通过 URL 获取远程页面的标题。该代码适用于 FFX,但不适用于 chrome。有人有想法么?

$(document).ready(function(){
    $("title").remove();
    $("head").load("http://www.latentmotion.com title");
});
4

1 回答 1

3

在这里,这适用于所有浏览器

$.get("http://www.latentmotion.com", function(response){
     alert((/<title>(.*?)<\/title>/m).exec(response)[1]);
  });

你可以在这里测试它http://jsfiddle.net/N7D5r/

如果你想完全避免使用 jQuery

var getXhr = (function () {
    if ("XMLHttpRequest" in window) {
        return function () {
            return new XMLHttpRequest();
        };
    }
    else {
        var item = (function () {
            var list = ["Microsoft", "Msxml2", "Msxml3"],
                i = list.length;
            while (i--) {
                try {
                    item = list[i] + ".XMLHTTP";
                    var obj = new ActiveXObject(item);
                    return item;
                }
                catch (e) {}
            }
        }());
        return function () {
            return new ActiveXObject(item);
        };
    }
}());

var req = getXhr();
req.open("GET", "http://www.latentmotion.com", true);
req.onreadystatechange = function () {
    if (req.readyState == 4) {
        if (req.status >= 200 && req.status < 300) {
            // here you retrieve the title
            var title = (/<title>(.*?)<\/title>/m).exec(req.responseText)[1];
            alert(title);
        }
        req.onreadystatechange = null;
        delete req.onreadystatechange;
    }
};
req.send();
于 2010-05-26T13:04:50.027 回答