...

View Full Version : external javascript



shells
07-09-2005, 06:51 AM
I know this might be an odd question, but I haven't been able to find the answer anywhere.

I have an enormous amount of javascript in my <head> section. I want to put them in an external file.

I know I need to save them in Notepad as a text file. Here's what I don't know.

Do I need to upload the file to the server first for it to work or should I be able to view it in the preview mode?

I saved a file as mysite.js When I looked at the file in my documents in says it is JScript. Does that make any difference. And if it does, what should I do about it.

I know not to use the <script></script> tags in the text file. One tutorial said to put comment tags <!-- at the start of the script and --> at the end. It was the only tutorial I found that mentioned that. Do I need to use the comment tags in the text file?

Thanks,
Shells

vwphillips
07-09-2005, 08:11 AM
Its realy easy and vvery useful

<!-- at the start of the script and --> are opional

first try it locally on your pc with no file path in the same directory as the target HTML Page

<script language="JavaScript" src="fred.js" type="text/javascript"></script>

one you are happy it is sometimes good to put the External JavaScripts
in a common directory for use by a number of documents. Then changes only need to be done once

"Say

<script language="JavaScript" src="MyJavaScripts/fred.js" type="text/javascript"></script>

to be used by a number of documents

as an aside
if you are going to post a problem with a page javascript
normally best to embed the script in the document, saves helpers hunting down the script.

shells
07-09-2005, 09:27 AM
Thanks vw for the answer on the comment tags. That helped alot.

Somethings take a little while to sink in...hehe

Do I have to upload the file first for it to work or should it work locally?

And

When I save the file as a .js why does the folder icon say it's a JScript?

This is what I'm using in the <head>

<script language="JavaScript" src="MyDocument/filename.js">
</script>

One more thing... Why can't I justbuild it in my document root, like I do the css and php? I know...I know... I can use css and php, but can't figure out little ole javascript includes :rolleyes: go figure

shells

vwphillips
07-09-2005, 09:41 AM
Do I have to upload the file first for it to work or should it work locally?


first try it locally on your pc with no file path in the same directory as the target HTML Page



When I save the file as a .js why does the folder icon say it's a JScript?

PCs display the file type in all manner of ways



Why can't I justbuild it in my document root, like I do the css and php?

you can but


to be used by a number of documents.
Then changes only need to be done once

shells
07-09-2005, 01:30 PM
thanks again vw,

It keeps saying it can't display page.

The js file won't save under Text Document (*txt). It saves under "All Files"


I know it should link just like an image tag, but for some reason it isn't.

Thanks for your patience with me :)

Shells

vwphillips
07-09-2005, 01:55 PM
Dont know what editor you are using

For notepad save as a .txt then change extension to .js

mrruben5
07-09-2005, 03:47 PM
Turn off "hide extensions for known files" in folder options.

Also, remove the language="JavaScript". It's not needed. type="text/javascript" is enough.

vwphillips
07-10-2005, 11:59 AM
ment to say

if you are looking for a good text editor with syntax coloring

try NotePad2 (free!)

http://www.flos-freeware.ch/notepad2.html

shells
07-11-2005, 10:49 AM
I'm using Notepad.

I saved it as .txt and then changed it to .js with the same outcome.

I unchecked the "hide known extensions" in folder option and tried to save a different piece of script in Notepad. It's still saving in "All Files", says it is JSript script, and it says it is 204 bytes.

It is a simple countdown script. Here is the script and how I have it entered in Notepad.

<!--Hide from older browers
//This function calculates the number of days until Christmas
function XmasDays(Month, Day, Year) {
var DayCount=(25-Day) + 31 + 30;
return DayCount;
}
//Stop hiding-->

PS vm, I checked out Notepad2 and I think I'm going to love it!

Shells



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum