如何使用Javascript检查页面是否存在

How to check if page exists using Javascript?

本文关键字:是否 存在 检查 何使用 Javascript      更新时间:2023-09-26

我正在使用Javascript,我想检查(另一个域的)页面/url是否存在

我试过了,但它不起作用

    $.ajax({
    type: 'HEAD',
    url: 'http://google.com',
success: function() {
        alert('1');
},
error: function() {
        alert('0');
}
});

它显示了这个错误

XMLHttpRequest无法加载http://google.com/.不请求的上存在"Access Control Allow Origin"标头资源原点'http://localads-4u.com因此不允许通道

注意:测试的脚本是在线的

我正在使用Javascript,我想检查(另一个域的)页面/url存在)

如果唯一的要求是检查外部资源是否可用,则来自AJAXNo 'Access-Control-Allow-Origin' header is present on the requested resource.响应的一个可能的解决方法是改为请求.html document<img>元素的src处的url。检查GET http://example.com/ net::ERR_NAME_NOT_RESOLVEDonerror处理程序

var img = new Image;
img.onload = function(e) {
  console.log(this, this.src)
}
img.onerror = function(e) {
  console.log(e, this)
};
img.src = "http://example.com";