Click to See Complete Forum and Search --> : Putting JS code in a seperate file and linking to it
Pretty much summed up the question in the title.
I've got a rather large, crude javaScript database in the top section of my html script, as well as clumps of CSS and other code. I want to know if, for organization's sake, it's possible to off-load the JS into a seperate file and then link to it in the same way you would a relative style sheet.
If this can be done with a snip of code then great. If not, I'll just leave it in the main html file.
Either way please excuse my ignorance and many thanks in advance.
Drop all your JavaScript in myjavascript.js without <script> tags.
Link to it from the html document (usually in the head)
<script type="text/javascript" src="myjavascript.js"></script>
7stud
03-25-2005, 03:17 AM
<script type="text/javascript" src="my_file.js"></script>
The file will act as if it is inserted between the <script> tags. The file should contain only pure javascript. For instance:
main.htm
-------
<head>
...
...
<script type="text/javascript" src="my_file.js"></script>
</head>
my_file.js
----------
alert("hello");
Thanks very much indeed both of you.
I was putting <script> tags into the js file and I think that must have been the problem.
I'm sure this was a thread waste for most experienced coders, but I'm sure there must be one or two nu's who need to know this.
The error of placing <script> tags in the external file is a common occurrence. ;)
herodote92
03-25-2005, 07:05 AM
Yes, I often wonder what percentage of one's time one spoils with such stupid syntactic problems - be it in Javascript or in any other language, and especially for people who often change from one language to another.
I often dream of a programmer-friendly environment in which the programmer could really concentrate on functionalities and algorithms, and forget about that darned syntax, that is not of any interest to anybody except to the language conceiver and to the smart guys who will giggle: ah ah, but I knew, that you should have put a comma, or a semicolon, or a bracket, or whatever, and you didn't, ah ah.
This should be possible I think. After all, this is the 3rd millenium... Stone Age is supposed to be over.
(Somebody will probably answer me that this exists already, but as far as I'm concerned, I can only see that we go on and on losing our time on syntax, whatever the environment - this is everyday experience).
Every language needs syntax, the more complex it is the more syntax you require.
I assume from your comments you don't have the developer bar (http://www.chrispederick.com/work/firefox/webdeveloper/) in FF set to 'enable strict JavaScript warnings' nor do you check you scripts with JSlint (http://www.crockford.com/javascript/jslint.html)
Correct syntax is important.
7stud
03-25-2005, 08:59 PM
dup.
7stud
03-25-2005, 09:01 PM
This should be possible I think. After all, this is the 3rd millenium... Stone Age is supposed to be over.
Yes, I agree, there should be no such thing as a computer programmer--instead you should just be able to say:
doEverythingIwant();
But then, there would be bulletin boards full of questions like,
why doesn't this work:
doEverythingIwant(
And, then when you explain that a closing parenthesis is needed, they would say something like:
Why is the syntax so picky--I should be able to just type a "d" and the computer should do everything I want. After all this isn't the 25th century anymore.
herodote92
03-25-2005, 11:02 PM
I'm afraid I disagree. The reasons to keep such problems alive are often bad reasons: the power of the programmer would be endangered if anybody would have easy, friendly tools at hand, at would be able to write his own programs or scripts.
Syntax is not important, in my opinion. Concepts are important. Understanding the user's problem is important. Figuring an algorithm to solve it is important. Ergonomy is important.
But there is absolutely not the faintest reason while, say, the concatenating function would be "+" in Javascript, "." in PHP et ":" in the Basic I use, to take a trivial example. What is important is understanding what concatenation means, and what you can use it for.
Oh well, all this will change one day, and our grandchildren will look at us like they will look at Pithecanthrops.
I'll be off for the week-end to forget a little about syntax problems. :) have a nice time.