What is the right way to write my script 'src' url for a local development environment?
Write the src tag for calling the js file as
<script type='text/javascript' src='../Users/myUserName/Desktop/myPage.js'></script>
This should work.
I believe the browser is looking for those assets FROM the root of the webserver. This is difficult because it is easy to start developing on your machine WITHOUT actually using a webserver ( just by loading local files through your browser)
You could start by packaging your html and css/js together?
a directory structure something like:
-yourapp
- index.html
- assets
- css
- js
- myPage.js
Then your script tag (from index.html) could look like
<script src="assets/js/myPage.js"></script>
An added benifit of packaging your html and assets in one directory is that you can copy the directory and give it to someone else or put it on another machine and it will work great.
This is an old post but...
You can reference the working directory (the folder the .html file is located in) with ./
, and the directory above that with ../
Example directory structure:
/html/public/
- index.html
- script2.js
- js/
- script.js
To load script.js from inside index.html:
<script type="text/javascript" src="./js/script.js">
This goes to the current working directory (location of index.html) and then to the js folder, and then finds the script.
You could also specify ../
to go one directory above the working directory, to load things from there. But that is unusual.