[javascript] Javascript Src Path

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

<SCRIPT LANGUAGE="JavaScript" SRC="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!!

This question is related to javascript

The answer is


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="../clock.js"></SCRIPT>

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


This works:

<script src="/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.


Piece of cake!

<SCRIPT LANGUAGE="JavaScript" SRC="/clock.js"></SCRIPT>

Try:

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

Note the forward slash.


If you have

<base href="/" />

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


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="/js/clock.js" type="text/javascript"></script>

src="/clock.js"

be careful it's root of the domain.

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


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="/javascript/clock.js"></script>

If it's on your domain root directory:

<script language="JavaScript" src="/clock.js"></script>