Shawn H
Shawn H

Reputation:

Javascript Src Path

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?

Upvotes: 30

Views: 172166

Answers (9)

ian selley
ian selley

Reputation: 43

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>

Upvotes: 4

ZiiMakc
ZiiMakc

Reputation: 36826

If you have

<base href="/" />

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

Upvotes: 3

Sanil
Sanil

Reputation: 5

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.

Upvotes: -3

Eric
Eric

Reputation: 97575

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.

Upvotes: 4

brettkelly
brettkelly

Reputation: 28205

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>

Upvotes: 5

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827236

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>

Upvotes: 12

SilentGhost
SilentGhost

Reputation: 319551

src="/clock.js"

be careful it's root of the domain.

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

Upvotes: 3

Rodrigo
Rodrigo

Reputation: 4395

Piece of cake!

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

Upvotes: 3

Crescent Fresh
Crescent Fresh

Reputation: 116980

Try:

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

Note the forward slash.

Upvotes: 29

Related Questions