Forum Moderators: open

Message Too Old, No Replies

How to call a JavaScript Function from another file

javascript

         

rodriguez1804

9:01 pm on Apr 13, 2010 (gmt 0)

10+ Year Member



Hey guys,

I have two files:

1.) index.php with a form

2.) validation.js with a validation function
ex.) validate(){blah blah...}

I want to call this function from the index page, but every time I call it like this: if(validate()==true), then I get an error (validate function undefined!?)

How do I properly call a js function from another file? Thanks.

physics

9:16 pm on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have a line like this in the <head> of index.php?

<script language="JavaScript" src="validation.js"></script>

Fotiman

1:29 pm on Apr 14, 2010 (gmt 0)

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



Note, the language attribute is invalid (and the type attribute is required, so use type instead), and the best place for script includes is at the END of the document, just before the closing </body> tag.

...
<script type="text/javascript" src="validation.js"></script>
</body>

john_k

8:43 pm on Apr 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fotiman - Why do you say that the best place for the script tag is at the end of the document?

physics

8:45 pm on Apr 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What Fotiman said :)

Fotiman

9:03 pm on Apr 14, 2010 (gmt 0)

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



For performance reasons. When browsers load script files, they "block" all other downloads, so each script will load one at a time. Imagine it like this:

Suppose you have a document with a script that takes 500ms to load, and you have 4 images that each take 25ms to load and the rest of your content takes 400ms to load. So, 1000ms total (actually, browsers can download multiple images at once, so in reality it would be even faster, but this is just an example). If you put your script at the bottom, then the images and content become available after 100ms + 400ms, giving the appearance of a more responsive UI. If your script is at the beginning, the browser will not even start loading content or images until it has finished the script, so you've added a 500ms lag to the UI, making it appear less responsive.

Here's more info if you're interested:
[developer.yahoo.net...]