You have the order of things backward. As the page loads, any bare JS
function calls will be run as soon as they are reached. So, when the
line with callit() is reached, the JS engine will attempt to run the
function immediately. However, the function has yet to be defined.
That's because you have the definition inside of $(document).ready(),
which will not be evaluated until the document has fully loaded.

So, the answer is to not put the callit() definition inside of
$(document).ready(). Just ensure that it is defined before the
function call.

 <script type="text/javascript">
     $(document).ready(function() {
       // whatever stuff needs to occur when the doc is loaded
     });

     // will be defined immediately
     function callit() {
        alert("Hello world!");
     }
 </script>
 <script type="text/javascript">
     // will be called immediately
     callit();

 </script>


And, if you'd like callit() to be run only when the document is fully
loaded, just wrap the call in another $(document).ready() block:


 <script type="text/javascript">
     $(document).ready(function() {
       // whatever stuff needs to occur when the doc is loaded
     });

     // will be defined immediately
     function callit() {
        alert("Hello world!");
     }
 </script>
 <script type="text/javascript">
     $(document).ready(function() {
       // will be run when doc is loaded
      callit();
     });
 </script>

You can have as many different  $(document).ready() as you want. All
they do is, essentially, assign bits of code that should be run once
the document is fully loaded.

On Tue, Dec 15, 2009 at 8:23 PM, Wendi Turner <wenditur...@gmail.com> wrote:
> I am trying to call a jQuery loaded javascript function by a
> static javascript function on the html page... and I cannot get it to
> trigger.
>
> If I tie the jQuery function to $('a').click( loadSomething );
>
> Then it works.  But I want to be able to call the function from a resident
> script???
>
>
> <html>
> <head>
>  <script type="text/javascript">
>      ... added/generated by c#
>      $(document).ready(function() {
>          function callit() {
>              alert("Hello world!");
>          }
>      ...
>      });
>
>  </script>
>  <script type="text/javascript">
>      ... on asp.net Default.aspx page
>      callit();
>
>  </script>
>  </head>
>  <body>
>  </body>
>  </html>

Reply via email to