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

If I have an HTML page with a script tag like this:<SCRIPT SRC=".xxx.js"><SCRIPT&g

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742265425a4411694.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信