user555303
user555303

Reputation: 1374

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

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?

Upvotes: 5

Views: 4552

Answers (4)

Edson Horacio Junior
Edson Horacio Junior

Reputation: 3143

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.com/route/file.jpg

Upvotes: 0

AsimRazaKhan
AsimRazaKhan

Reputation: 2202

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.

Upvotes: 0

Rune FS
Rune FS

Reputation: 21752

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

Upvotes: 0

James Thorpe
James Thorpe

Reputation: 32212

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.

Upvotes: 5

Related Questions