Forum Moderators: open
I have created a Java navigation for a clients web site - however I would like to reduce the websites code and move the Java script within the page to an external file (script.js). This also makes life easier to maintain the script later if needed.
Does anyone know of a good tutorial, or can direct me in how to do this. I am greatful for any advice anyone has to offer.
Thanks!
GoV
The code is <script type="text/javascript" language="javascript" src="filename.js"></script>
Where filename if your script. Make sure to have only the code of the js in the file, i.e. remove <script language="JavaScript"> from the actual JS file
Hope this helps!
Just one more question: Do I take the java script in the html and the the code from the HTML Body and drop it into the .js file also?
I guess what I mean is everything that makes the Nav work - put it in the .js file?
Thanks again for the help!
GoV
1.) In the Head there is the Java script that contains everything that runs the Navigation.
2.) In the Body there is the HTML that contains the links, and some functions that make the actual Nav bar.
What I have to do?
In the real world - it looks like I can only put the Java script in the script.js file, and leave the HTML on the web page. The HTML would link back to the script.js file. Am I on that right?
------------------------------
In my dream world it would be nice to be able to dump both the Java and the HTML all in one file. (or is that possible?) A good nav I guess would be one that was pure Java script not 1/2 and 1/2 ;)
----------------------------------
Let me know if I understand everything right. If so I thank everyone for their time.
~GoV
I think I see your underlying thinking. You want to move the HTML for the navigation off the page also? My guess here is for easier administration later (read edits/changes). I do just this. I make the HTML an SSI (server side include page). That way on the actual page you only have the external JS file call and you only have a SSI call to the HTML part of the navigation. Makes life eaier on the admin side later.
Did I read inbetween the lines right here?
brian
Thanks...
GoV
Also, Javascript is not the same as Java. They are very different languages.
Keeping the javascript in a separate .js file is a good idea even beyond keeping the HTML file small.
With the script in an external file you can use it in multiple HTML files with only one line of code to add to each HTML file.
Also, you don't have to change 50 files with a change to the javascript - only 1.
With external files though, you need to be very good with variables as the Document Object Model does not tranfer across files.
Good luck with Javascript - it's a fun language. I'd recommend the O'Reilly book for a great reference.
This is a good place to post discussions on scripting.
[web-source.net...]
I'm no techie, but I learned how to put my drop-down menus and my Atomz site search boxes into external .js scripts. This makes life easier in several ways, and also means that I can put a search box on every page without a lot of code bloat.