简体   繁体   English

使用Javascript转到本地URL

[英]Go to local URL with Javascript

Same question as here but I need to go to local URL's in Firefox 同样的问题在这里 ,但我需要去当地的URL在Firefox

I tried with code like 我试过像代码一样

var url = "file:///E:/Test/Test.htm";
window.location.href = url;

but id didn't work. 但是id不起作用。 Tried to go with window.location = url; 尝试使用window.location = url; and also tried with url = "file://E:/Test/Test.htm"; 并尝试使用url = "file://E:/Test/Test.htm"; (double "/" instead of triple "/") and still doesn't work. (双“/”而不是三“/”)仍然不起作用。

Thanks 谢谢

When I try this: 当我尝试这个:

window.location.href = "file:///C:/Users/Cerbrus/Documents/SomeFile.js"

(Yes, it is a valid path.) (是的,这是一条有效的道路。)

Chrome throws me this error: Chrome会抛出此错误:

Not allowed to load local resource: file:///C:/Users//Documents/File.js 不允许加载本地资源:file:/// C:/Users//Documents/File.js

This is because JavaScript does not have access to local files (due to it being sandboxed), and you're setting the new url with JavaScript. 这是因为JavaScript无法访问本地文件(由于它是沙箱),并且您使用JavaScript设置新的URL。
"SandBoxed" means a technology has restricted (or no) access outside a certain set of bounds. “SandBoxed”意味着技术限制(或不限制)在特定边界之外的访问。 In the case of browsers, this means that the code that runs on the page can not access files on your system (Otherwise, it would be easy to "steal" data, by just having a look at the user's file system). 对于浏览器,这意味着在页面上运行的代码无法访问系统上的文件(否则,通过查看用户的文件系统,很容易“窃取”数据)。

However , 但是

Say, I have 2 files: 说,我有2个文件:

C:/Test/Test.htm C:/Test/Test.htm
C:/Test/Test1.htm C:/Test/Test1.htm

Test.htm contains only this: Test.htm仅包含以下内容:

<script>
    window.location = "file:///C:/Test/Test1.htm";
</script>

This will actually redirect to Test1.htm , since the target file is on the same domain as where the source file's from. 这实际上将重定向到Test1.htm ,因为目标文件与源文件所在的域位于同一个域中。

I guess its not allowed to load local resource from javascript 我想它不允许从javascript 加载本地资源

Unless you have a local http server running: 除非您运行本地http服务器:

var url = "http://localhost/MySite/Default.aspx";
window.location.href = url;

It will work 它会工作

You cannot access the file from the local system. 您无法从本地系统访问该文件。 Since the Browser works in the sandbox mode and you cannot breach the sandbox and reach the local file system since it would violate the security. 由于浏览器在沙箱模式下工作 ,您无法破坏沙箱并到达本地文件系统,因为它会违反安全性。 Either try to directly load using an AJAX request else what you are trying to do is not possible due to sandbox restrictions and also does not comply with the security policies. 尝试使用AJAX请求直接加载,否则由于沙箱限制而无法执行您要执行的操作,并且也不符合安全策略。

window.location.href = window.location.pathname +(您的本地文件名或路径)

window.open(url); // here url can be anything

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM