Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 验证文件是否存在_Javascript - Fatal编程技术网

Javascript 验证文件是否存在

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

在新选项卡中打开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 = '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不支持跨域。您必须在服务器上执行。我想在本地检查它,而不是在服务器上,而且我认为没有一种方法可以像复制