admin管理员组文章数量:1331091
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?
- 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
4 Answers
Reset to default 5I 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
本文标签:
版权声明:本文标题:javascript - When does "src" in script tag reference a resource on local filesystem (vs. non-local)? - Stack O 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742265424a2443300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论