How do i put my script in an extern file?

K

Konrad Den Ende

I have a script placed somewhere in the body of my HTML-doc.
I noticed that i'll be using the very same script in a large number
of document (all of them as the script regards the time of the
most recent edition of the given file).

I could place the script in every single document BUT:
1. I'm far too lazy to go through 100 documents doing one and the
same thing. That's for monkeys, not lazy humans.
2. Even if i wasn't so lazy, i'd have to reedit every single
document once i made a small change to the script. That's for
crazy monkeys!

So, how can i place the script in a file and how do i call the
file from my HTML-code?

--

Kindly
Konrad
---------------------------------------------------
May all spammers die an agonizing death; have no burial places;
their souls be chased by demons in Gehenna from one room to
another for all eternity and more.

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy
---------------------------------------------------
 
J

JW

It's easy. In the HTML-file you put :
<script language="JavaScript" src="yourscriptfile.js"></script>

And you place your scriptfile in the "yourscriptfile.js"
JW
 
K

knoak

Just put

<script language="javascript" src="nameofyourscript.js"></script>

between the <head> tags...

The page will automatically read it as if it was in the
page itself...

Greetings
 
K

Konrad Den Ende

Thanks, Will try that tonight.

--

Kindly
Konrad
---------------------------------------------------
May all spammers die an agonizing death; have no burial places;
their souls be chased by demons in Gehenna from one room to
another for all eternity and more.

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy
---------------------------------------------------
 
E

Evertjan.

Konrad Den Ende wrote on 21 jun 2004 in comp.lang.javascript:
Thanks, Will try that tonight.

This is no email, this is usenet posting.
So, for the benefit of all the other readers,
quote a significant [in the quality sense] part of the mail
you are referring to, [without resorting to topposting]
 
L

Lasse Reichstein Nielsen

JW said:
It's easy. In the HTML-file you put :
<script language="JavaScript" src="yourscriptfile.js"></script>

The "type" attribute is required on script elements in HTML 4. The
"language" attribute is deprecated and can safely be omitted.
So, the recommended (and validating) way to write it would be:

And you place your scriptfile in the "yourscriptfile.js"

(and remember not to have <script> tags in the javascript file)

/L
 
K

Konrad Den Ende

So, the recommended (and validating) way to write it would be:
<script type="text/javascript" src="yourscriptfile.js"></script>

I have placed following in the <head>
<script type="text/javascript" src="scripts.js"></script>
and made the script itself to a function returning a string as in
function getSome () { ... }
Then i call it by
<script>document.write (getSome ());</script>
but i get the message that the site is loaded but it contains errors.
I can't see the text i'm supposed to, either. What did i miss?



--

Kindly
Konrad
---------------------------------------------------
May all spammers die an agonizing death; have no burial places;
their souls be chased by demons in Gehenna from one room to
another for all eternity and more.

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy
---------------------------------------------------
 
K

Konrad Den Ende

Also check if you can read the .js file directly from your
server, maybe you must update/change the index.cache files

Hehe, it helps if one point to the right directory. Stupid mistake...
I put the file in an other directory (though one with the same
name but different path).

Nevertheless, i can't keep wondering about an ambigousity i see
here. What happens if i have two different functions stored in
two different files, yet call them the same name. Which one will
the computer thing i'm refering to?

--

Kindly
Konrad
---------------------------------------------------
May all spammers die an agonizing death; have no burial places;
their souls be chased by demons in Gehenna from one room to
another for all eternity and more.

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy
---------------------------------------------------
 
R

Richard Cornford

Richard said:
Konrad Den Ende wrote:
The data1.js file contains the following (you'll notice a function
inside it too, but you can of course remove it);

<!--
^^^^

You have put this in an external JS file? Why? In javascript it is -
less than NOT pre-decrement - and a syntax error as there are not
operands for the operations.

Richard.
 
K

Konrad Den Ende

<!--
^^^^

You have put this in an external JS file? Why? In javascript it
is - less than NOT pre-decrement - and a syntax error as there
are not operands for the operations.

Do you mean that it's not the right syntax for commenting out
a piece of code? What should be used?

--

Kindly
Konrad
---------------------------------------------------
May all spammers die an agonizing death; have no burial places;
their souls be chased by demons in Gehenna from one room to
another for all eternity and more.

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sense to be lazy
---------------------------------------------------
 
R

Richard Cornford

Konrad said:
Do you mean that it's not the right syntax for commenting out
a piece of code? What should be used?

Javascript syntax allows two forms of comments:-

// end of line comments using two slashes

- and:-

/* multi-line comments
surrounded by slash asterisk - asterisk slash.
*/

The character sequence used resembles an HTML opening comment tag, which
has alternative interpretations in javascript. The origin of this
nonsense is in a technique for hiding the content of script elements
from browsers that did not understand the meaning of SCRIPT elements and
so would include the contents of a script element along with the text
contents of an HTML page. But browsers that do not understand SCRIPT
elements are long since dead and gone, and it never was necessary to
attempt to use such tricks in an external JS file as a browser that does
not understand SCRIPT elements does not know that there is a JS file
associated with the SCRIPT element and so will not download it.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top