Forum Moderators: open

Message Too Old, No Replies

External Java Scripts

Need to make Java Navigation an external script - how do I do this?

         

GoV

3:13 am on Mar 6, 2002 (gmt 0)



Greetings - my first forum question.

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

brotherhood of LAN

3:34 am on Mar 6, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to Webmasterworld Gov! Youve come to the right place

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!

GoV

3:54 am on Mar 6, 2002 (gmt 0)



brotherhood of LAN - Thank you very much. Sounds easy enough, and will give it a shot...

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

jatar_k

3:57 am on Mar 6, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if I understand what you are asking
you just take what is between your script tags in the header (or wherever you put it) and dump it into the .js file. All of your function calls etc stay in your html page.

GoV

5:03 am on Mar 6, 2002 (gmt 0)



Close - and hope I am not making this more complicated that it should be. (ducking for flying objects)

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

brotherhood of LAN

5:45 am on Mar 6, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well Gov, I know what I told you, as for including HTML I do not know, someone else in here will.

However, theres nothing wrong with trial and error!! You have a 50/50 chance of getting it right first time

Reflect

1:11 pm on Mar 8, 2002 (gmt 0)

10+ Year Member



Hi,

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

GoV

4:11 am on Mar 10, 2002 (gmt 0)



Reflect
Perfect! This is what I want to do, so I will give this a try after it sounds workable :)

Thanks...
GoV

txbakers

4:42 am on Mar 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Keep in mind that SSI doesn't work with .HTM or .HTML extensions - you'll need a .SHTM or .SHTML or .ASP extension to use file includes.

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.

buckworks

5:20 am on Mar 10, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Here's a tutorial that I found helpful:

[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.