PDA

View Full Version : Can you send variables to external.js


skeatt
12-02-2002, 11:17 AM
I've made a function that's used in all my pages and figured it was a good candidate for putting in a .js file. However there is one problem...each page has a different initialization, different image folder etc. for the function.

var imgDir = "images_main/";
var imgFilePrefix = "image";
var imgPath = imgDir + imgFilePrefix;
var imgNum = 18;

I though if I leave this bit in my page it might put it together
when the .js file is loaded. NOT:eek: .... "error" imgNum undefined

or am I asking to much of the .js method of loading javascript?

I also noticed if I leave another function on my page, when it gets called it errors with "object expected", put it in the .js file and no errors

function go(url) {
window.location.href=url; }

Does this mean once you define an external.js, all functions have to go in there?

Borgtex
12-02-2002, 11:42 AM
Define the variables before calling the .js:

<script>
var imgDir = "images_main/";
var imgFilePrefix = "image";
var imgPath = imgDir + imgFilePrefix;
var imgNum = 18;
</script>
<script language="JavaScript" src="yourjsfile.js" type="text/JavaScript"></script>

For your second question, you can have functions defined both in your page and a .js file. The message "object expected", probably means that you made an HTML or Javascript syntax error and the function isn't defined.

skeatt
12-04-2002, 02:09 PM
:thumbsup: Thanks Borgtex,
Once I placed my global variable before the *.js definition all went great. This is how I formatted it:-

<script language="JavaScript" type="text/JavaScript">
// <!-- hide
var imgDir = "images_nav/"; // global variables
var imgFilePrefix = "image";
var imgPath = imgDir + imgFilePrefix;
var imgNum = 7; // number of images to preload

/* ------ go to URL ---------------------- */
function go(url) {
window.location.href=url; }

// --> end hide
</script>
<script language="JavaScript" src="myjsFile.js" type="text/JavaScript"></script>

Is it necessary to use... type="text/JavaScript" in the <script> tags?

I found what was causing my "object expected" error, I put the "go" function originally between the tags that defined the
myjsFile.js , of course it went looking in the .js file for it. Once I put it above that, it too worked.

Borgtex
12-04-2002, 05:01 PM
Originally posted by skeatt
Is it necessary to use... type="text/JavaScript" in the <script> tags?


It usually will work without "type" and even without "language", but it's a good habit to use them

whammy
12-05-2002, 12:58 AM
Actually, if you're concerned at all about standards, the "language" attribute is deprecated in XHTML... this is correct:

<script type="text/javascript">

However, since javascript is the default client-side scripting language, just using <script> is usually fine... (I haven't tested to see if that validates, though!).

However if by some chance you were also using say VBScript on a page, then you would need to declare the type anyway. As BorgTex said, it's a good habit - but the "language" attribute is unnecessary and deprecated, and your scripts will work just fine without it. :)