javascript 如何在javascript中获取当前文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18612460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:35:20  来源:igfitidea点击:

How to get the current file path in javascript

javascriptjquery

提问by shoaib

can i get the full path of my html page through javascript eg My Index page is at

我可以通过 javascript 获取我的 html 页面的完整路径,例如我的索引页面位于

         d:/somefolder/anotherfolder/index.html

so can i get this path

所以我可以走这条路吗

Thanks

谢谢

回答by MD Sayem Ahmed

You can only access your current file path relative to your host URL, you cannot access the folder path of that file.

您只能访问相对于您的主机 URL 的当前文件路径,您不能访问该文件的文件夹路径。

This is restricted in JavaScript due to security reasons. If that was allowed, malicious scripts could easily read your server's internal folder structures, which is bad.

由于安全原因,这在 JavaScript 中受到限制。如果允许,恶意脚本可以轻松读取服务器的内部文件夹结构,这很糟糕。

回答by defau1t

On a Local Machine it is not possible, however you could use:

在本地机器上这是不可能的,但是您可以使用:

document.location.href;

to get the URL, where your file is hosted

获取托管文件的 URL

回答by amdorra

no you can't browser don't allow javascript to access the file paths due to security reasons

不,由于安全原因,您不能浏览器不允许 javascript 访问文件路径