34

I'm working on a local environment and I'm not sure if I've written my src URl correctly because my functions aren't working. The bold script tag has the src in question.

<!DOCTYPE html>
<html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script src="http://www.parsecdn.com/js/parse-1.2.2.min.js"></script>

    **<script src="/Users/myUserName/Desktop/myPage.js"></script>**

    </head>
    <body>
        <div id="mainDiv">
            <p><a href="#">anchor</a></p>
        </div>
    </body>
</html>
Spilot
  • 1,495
  • 8
  • 29
  • 55
  • 8
    Open your `js` file in browser, then copy absolute path from browser's URL. It maybe like that: file:///C:/Users/User12/Desktop/underscore.js – Ikrom May 21 '13 at 18:56

3 Answers3

39

Write the src tag for calling the js file as

<script type='text/javascript' src='../Users/myUserName/Desktop/myPage.js'></script>

This should work.

ARC
  • 437
  • 3
  • 3
  • 1
    I did this, but for some reason when I load the javascript page, it thinks that I want to make a get request to the baseURL + "js/index.js". Am I doing something wrong? – Alaska Aug 23 '21 at 19:56
19

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.

dm03514
  • 54,664
  • 18
  • 108
  • 145
4

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"></script>

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.

RileyManda
  • 2,536
  • 25
  • 30