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

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

最佳答案

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

$.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();

10-06 04:04