Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to write my script 'src' url for a local development environment?

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>
like image 757
Spilot Avatar asked May 21 '13 18:05

Spilot


People also ask

How do I reference a local JavaScript file in HTML?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

How do you define a src script?

Definition and UsageThe src attribute specifies the URL of an external script file. If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again.

Where should we put link and script and why?

The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load. Generally, JavaScript code can go inside of the document <head> section in order to keep them contained and out of the main content of your HTML document.

Where do you put a script link?

You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.


2 Answers

Write the src tag for calling the js file as

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

This should work.

like image 76
ARC Avatar answered Oct 19 '22 19:10

ARC


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.

like image 19
dm03514 Avatar answered Oct 19 '22 18:10

dm03514