javascript java脚本的绝对文件路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6395367/
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
Absolute file path for java script
提问by Alex
I have a small javascript library written by myself. I want to reference it in my web application, but it doesn't work
我有一个自己编写的小型 javascript 库。我想在我的网络应用程序中引用它,但它不起作用
<script src='file:\C:\Path\To\Script\Script.js'></script>
Is it possible to reference javascript when all you know is the absolute path?
当您只知道绝对路径时,是否可以引用 javascript?
回答by KooiInc
The file:
url needs 3forward slashes, and the path also needs forward slashes:
该file:
URL需要3个斜杠,路径也需要正斜杠:
<script src='file:///C:/Path/To/Script/Script.js'></script>
This will ofcourse only work if you load the script within a html-file on your disk that's loaded in your browser.
这当然只有在您在浏览器中加载的磁盘上的 html 文件中加载脚本时才有效。
回答by Scott
Though I'm a Mac guy, my (limited) understanding of Windows security is that it has restrictions around running Javascript files "locally." Your best bet would be to copy the file to your web application's directory structure and reference it through a relative path.
虽然我是 Mac 用户,但我对 Windows 安全性的(有限)理解是它对“本地”运行 Javascript 文件有限制。最好的办法是将文件复制到 Web 应用程序的目录结构并通过相对路径引用它。
Not only will this provide greater reliability, but it's also one less thing to remember to include when deploying to a web server or somesuch. If the JS file is outside your app's folder structure, when deploying to a remote server you'll have to include it anyways.
这不仅会提供更高的可靠性,而且在部署到 Web 服务器或其他类似的东西时要记住的事情也少了。如果 JS 文件在您的应用程序的文件夹结构之外,则在部署到远程服务器时,您无论如何都必须包含它。