Javascript 验证文件是否存在
在新选项卡中打开HTML文件之前,我想验证它是否存在 如何使用纯JavaScript实现这一点 我将文件的位置存储在path变量中Javascript 验证文件是否存在,javascript,Javascript,在新选项卡中打开HTML文件之前,我想验证它是否存在 如何使用纯JavaScript实现这一点 我将文件的位置存储在path变量中 path = "F:\Folder_JS\File1.html"; //I want to check here whether there exists such a file or not before opening it new tab myWindow = window.open(path,'_blank'); 您可以通过js: var url = 'F
path = "F:\Folder_JS\File1.html";
//I want to check here whether there exists such a file or not before opening it new tab
myWindow = window.open(path,'_blank');
您可以通过js:
var url = 'File1.html'; //use file url not real path. ex: folder/File1.html
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
if(http.status!=404){
//file exist
}else{
//file not exist
}
我已经尝试过了,但它一直在生成一个错误:访问受限URI的权限被拒绝。我认为XMLHttpRequest方法不受欢迎。您必须从服务器上执行此操作。因为js不支持跨域。您必须在服务器上执行。我想在本地检查它,而不是在服务器上,而且我认为没有一种方法可以像复制