使用Javascript转到本地URL [英] Go to local URL with Javascript

查看:364
本文介绍了使用Javascript转到本地URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处相同的问题,但我需要去本地在Firefox中的URL



我尝试使用类似于

  var url = 文件:/// E:/Test/Test.htm; 
window.location.href = url;

但id无效。试着去 window.location = url; ,并尝试使用 url =file:// E:/Test/Test.htm; (double/而不是triple/)仍然不起作用

谢谢
当我试试这个:

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

(是的,这是一个有效的路径。)



Chrome会抛出这个错误:


不允许加载本地资源:file:/// C:/Users//Documents/File.js


这是因为JavaScript无法访问本地文件(由于它被沙箱化),并且您正在使用JavaScript设置新的url。

SandBoxed是指一种技术限制(或不允许)在一定范围之外的访问。在浏览器的情况下,这意味着在页面上运行的代码无法访问系统上的文件(否则,通过查看用户的文件系统,窃取数据很容易)但是,



说,我有2个文件:


$ b


$ b


C:/Test/Test.htm

C:/Test/Test1.htm




Test.htm 只包含这个:

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

这实际上会重定向到 Test1.htm ,因为目标文件与源文件来自同一个域。


Same question as here but I need to go to local URL's in Firefox

I tried with code like

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

but id didn't work. Tried to go with window.location = url; and also tried with 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:

Not allowed to load local resource: 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.
"SandBoxed" means a technology has restricted (or no) access outside a certain set of bounds. 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:

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

Test.htm contains only this:

<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.

这篇关于使用Javascript转到本地URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆