最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Absolute file path for java script - Stack Overflow

programmeradmin2浏览0评论

I have a small javascript library written by myself. I want to reference it in my web application, but it doesn't work

<script src='file:\\C:\Path\To\Script\Script.js'></script>

Is it possible to reference javascript when all you know is the absolute path?

I have a small javascript library written by myself. I want to reference it in my web application, but it doesn't work

<script src='file:\\C:\Path\To\Script\Script.js'></script>

Is it possible to reference javascript when all you know is the absolute path?

Share Improve this question asked Jun 18, 2011 at 10:30 AlexAlex 36.1k27 gold badges96 silver badges141 bronze badges 3
  • Is your web application locally or on the we or under a fake/real domain/ip? – Niklas Commented Jun 18, 2011 at 10:36
  • Niklas, it's hosted on localhost – Alex Commented Jun 18, 2011 at 10:39
  • 1 Any reason you can't copy it in your web application folder and reference it with relative path instead of absolute? – Aziz Shaikh Commented Jun 18, 2011 at 10:46
Add a comment  | 

2 Answers 2

Reset to default 15

The file: url needs 3 forward slashes, and the path also needs forward slashes:

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

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.

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.

发布评论

评论列表(0)

  1. 暂无评论