[Javascript] How to call scripts that are all in one file...?

Paul Novitski paul at novitskisoftware.com
Sat Feb 4 01:06:47 CST 2006


At 06:43 PM 2/3/2006, Rick Holcomb wrote:
>You could wrap each file in a function and then call the function when
>needed.
...
>Function routine1{
>         //Insert all code from the myroutine1.js file
>}
...
><script type="text/javascript">
>         routine1;
></script>
...

Oops: wrong answer.  To call/execute/run a function, you have to 
include the open & close parens after the function name:

         Function routine1{
                 //Insert all code from the myroutine1.js file
         }
         ...
         <script type="text/javascript">
                 routine1();
         </script>

Invoking the name of the function without the parentheses recalls the 
text of the function but doesn't execute that script.  For example, 
to cause a function to run when the page loads you can write:
         window.onload = routine1;
i.e., set the function window.onload() equal to the script of 
function routine1().  When the page finishes loading, the browser 
will execute window.onload().

To display the text of a function, write:

         alert(routine1);

To run a function and display its return value, write:

         alert(routine1());

For more on functions see:
http://www.croczilla.com/~alex/reference/javascript_guide/fcns.html


Here are two great fundamental references:

Core JavaScript Guide
http://www.croczilla.com/~alex/reference/javascript_guide/
[the JavaScript language itself]

Gecko DOM Reference
http://developer.mozilla.org/en/docs/Gecko_DOM_Reference
[how JavaScript works with the Document Object Model (DOM) of an HTML page]


Regards,
Paul 




More information about the Javascript mailing list