jQuery Can not $(...)。在Chrome中加载()头部标题 [英] jQuery Can't $(...).load() the head title in Chrome
本文介绍了jQuery Can not $(...)。在Chrome中加载()头部标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要通过URL获取远程页面的标题。代码在FFX中运行,但不是铬合金。任何人有任何想法?
I need to get the title of a remote page by URL. The code works in FFX, but not chrome. Anyone have any ideas?
$(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
And if you want to avoid jQuery altogether
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();
这篇关于jQuery Can not $(...)。在Chrome中加载()头部标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文