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

javascript - When does "src" in script tag reference a resource on local filesystem (vs. non-local)? - Stack O

programmeradmin6浏览0评论

If I have an HTML page with a script tag like this:

<SCRIPT SRC="./xxx.js"></SCRIPT>

Under what conditions would that ./xxx.js be gotten/accessed from the local filesystem?

I understand that the ./xxx.js URI/URL references the "the file named 'xxx.js' in the current directory", but when (under what conditions) will "current directory" mean the current directory on the local filesystem on which the client/browser is running?

Is the only situation where that would be the case be when the HTML file containing that <script> tag was retrieved from the local filesystem?

If I have an HTML page with a script tag like this:

<SCRIPT SRC="./xxx.js"></SCRIPT>

Under what conditions would that ./xxx.js be gotten/accessed from the local filesystem?

I understand that the ./xxx.js URI/URL references the "the file named 'xxx.js' in the current directory", but when (under what conditions) will "current directory" mean the current directory on the local filesystem on which the client/browser is running?

Is the only situation where that would be the case be when the HTML file containing that <script> tag was retrieved from the local filesystem?

Share edited Jan 6, 2016 at 14:07 Kyll 7,1518 gold badges44 silver badges64 bronze badges asked Jan 6, 2016 at 14:04 user555303user555303 1,3764 gold badges28 silver badges54 bronze badges 1
  • I'm curious to know where this question es from. Have you experienced mysterious phenomena that could only be explained by the browser erroneously trying to load from the local filesystem rather than the server? – Mr Lister Commented Jan 14, 2016 at 8:54
Add a ment  | 

4 Answers 4

Reset to default 5

I understand that the "./xxx.js" URI/URL references the "the file named 'xxx.js' in the current directory",

More correctly, it means it will reference the file named xxx.js relative to the current file.

That means it will look in the same directory that the file containing the <script> tag was loaded from. If it's the local file system, it will load it from there. If it was served from a webserver, it will issue a new request to the webserver for that file.

The protocol of the URI determines how files are requested. In your case the path is relative, so the protocol used when requesting the html page will be used. Let's say the html file is index.html if you then request it like this http://localhost/index.html. The your script file won't be server from the local file system, whereas if you request it like so file://path/index.html your script file will be served from the local file system

Oky, so..

The src attribute will look for the file in the path given to it is always be relative, when you load the web page locally it will look for that file in the given path, no matter if the path is local to you or if its local in a web server.

the path has to be correct

and remember if you put the javascript in the script tag along the src the javascript that you put on the script won't work.

When src value is a path, it will be considered local and may be a relative path or absolute path:

relative path examples

  • folder/subfolder/file.jpg
  • ../parent_folder/file.jpg

absolute path example

  • /root/folder/subfolder/file.jpg

When it is remote, src's value will be a URL like http://yourappsite./route/file.jpg

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论