Javascript Src Path

Hello I’m having some trouble with the following code within my Index.html file:

<SCRIPT LANGUAGE="JavaScript" SRC="https://stackoverflow.com/questions/1411627/clock.js"></SCRIPT>

This works when my Index.html file is in the same folder as clock.js. Both Index.html and clock.js are in my root folder.

But when my index.html is in these different directories clock.js does not load:

/products/index.html
/products/details/index.html

What can I put as the ‘SRC’ so that it will always look for clock.js in the root folder?

Thanks in advance!!

Try:

<script src="https://stackoverflow.com/clock.js"></script>

Note the forward slash.

Use an relative path to the root of your site, for example:

If clock.js is on http://domain.com/javascript/clock.js

Include :

<script language="JavaScript" src="https://stackoverflow.com/javascript/clock.js"></script>

If it’s on your domain root directory:

<script language="JavaScript" src="https://stackoverflow.com/clock.js"></script>

The common practice is to put scripts in a discrete folder, typically at the root of the site. So, if clock.js lived here:

/js/clock.js

then you could add this code to the top of any page in your site and it would just work:

<script src="https://stackoverflow.com/js/clock.js" type="text/javascript"></script>

Piece of cake!

<SCRIPT LANGUAGE="JavaScript" SRC="https://stackoverflow.com/clock.js"></SCRIPT>

This works:

<script src="https://stackoverflow.com/clock.js" type="text/javascript"></script>

The leading slash means the root directory of your site. Strictly speaking, language="Javascript" has been deprecated by type="text/javascript".

Capitalization of tags and attributes is also widely discouraged.

src="https://stackoverflow.com/clock.js"

be careful it’s root of the domain.

P.S. and please use lowercase for attribute names.

If you have

<base href="https://stackoverflow.com/" />

It’s will not load file right. Just delete it.

As you haven’t specified, I don’t know if you are working with flask (Python), but if you are, you have to put the JavaScript file in a directory called static. It should look something like this:

/ProjectName
  /static
    clock.js
  /templates
    index.html
  main.py

And then refer to the js file as following:

<script src="/static/clock.js"></script>

As your clock.js is in the root, put your code as this to call your javascript in the index.html found in the folders you mentioned.

<SCRIPT LANGUAGE="JavaScript" SRC="https://stackoverflow.com/questions/1411627/clock.js"></SCRIPT>

This will call the clock.js which you put in the root of your web site.

Leave a Comment