Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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 无法捕获Chrome';s';无法加载本地资源';try/catch块中出错_Javascript_Google Chrome_Try Catch - Fatal编程技术网

Javascript 无法捕获Chrome';s';无法加载本地资源';try/catch块中出错

Javascript 无法捕获Chrome';s';无法加载本地资源';try/catch块中出错,javascript,google-chrome,try-catch,Javascript,Google Chrome,Try Catch,我正试图通过将window.location.href设置为file://folder/,我可以在IE中完成,但在Chrome中无法完成 我的目标是在浏览器阻止本地文件访问时捕获,以便调用其他代码。Chrome的控制台显示“不允许加载本地资源:…”,但我无法用try/catch块捕获它 尝试: 函数OpenLocalResource(){ 试一试{ //在Chrome中失败(预期) window.location.href=file://folder/whatever/'; }捕捉(错误){ /

我正试图通过将
window.location.href
设置为
file://folder/
,我可以在IE中完成,但在Chrome中无法完成

我的目标是在浏览器阻止本地文件访问时捕获,以便调用其他代码。Chrome的控制台显示“不允许加载本地资源:…”,但我无法用
try/catch
块捕获它

尝试: 函数OpenLocalResource(){ 试一试{ //在Chrome中失败(预期) window.location.href=file://folder/whatever/'; }捕捉(错误){ //Chrome给出脚本错误“不允许加载本地资源:” //我期待着击中这个接球挡,但它没有 警报(“出错命中!”); } }
OpenLocalResource()这是一个安全设置,我想你抓不到它。但是,您可以使用cmd提示符启动chrome并添加--allow file access。

试试这个,但是您可能需要将映像src指向客户端上的实际映像

function noLocalAccess() {
    alert('Error hit!');
}
function OpenLocalResource() {
    var myImage=new Image();
    myImage.onerror=new Function("noLocalAccess()");
    myImage.src='file:///c:/';

    window.location.href = 'file://folder/whatever/';
}
OpenLocalResource();    

在使用window.location之前,使用ajax测试文件是否存在。我认为不可能以任何其他方式捕获错误,因为即使有错误,也会设置位置,并且JavaScript处理程序会从作用域中丢失

var xhr = new XMLHttpRequest();
xhr.open('GET', '/folder/whatever/myfile.html', true);
xhr.onreadystatechange = function () {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            window.location.href = '/folder/whatever/myfile.html';
        } else {
            alert("Error hit!");
        }
    }
};

要查看是否允许在chrome上使用它,请执行以下操作:

函数OpenLocalResource($file){ 变量img=$('
这对我很有效。注意:我必须修改条件以检查
err.originalEvent.srceelement.src
,因为
err.originalEvent.path
是未定义的。如果设置了,您可能需要添加一个检查,然后检查我提供的设置。否则的方式对您有效。基本上前提是chrome不使用t如果currentsrc元素是不允许的本地文件,则返回currentsrc元素