Go to local URL with Javascript

前端 未结 5 496
情话喂你
情话喂你 2020-11-27 07:02

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         


        
5条回答
  •  广开言路
    2020-11-27 07:13

    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:

    
    

    This will actually redirect to Test1.htm, since the target file is on the same domain as where the source file's from.

提交回复
热议问题