views:

4704

answers:

4

Dear all I am using javascript and jQuery My Mainfile has My.js,and ajax.

My.js has

  function build_one(){
         alert("inside build_one");
  }

My Mainfile has

       <script type="text/javascript">

             ..

             // here  I want to make call function defined in My.js build_one()
             ..

             // here the ajax call

             $.ajax({
      type:'POST',
      url: 'ajax.php',
      data:'id='+id  ,
      success: function(data){
       $("#response").html(data);                                                                                                    

      }
             });  

             ...

        </script>

Now My Question is How to make the build_one() function call before the ajax function?.Any suggestion?

+7  A: 

This should work:

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

    build_one();

    $.ajax({
            type:'POST',
            url: 'ajax.php',
            data:'id='+id  ,
            success: function(data){
                $("#response").html(data);
            }
         });
</script>
d4nt
You beat me to it. I was going to say the exact same thing. Even the code is the same.
sjbotha
Using language attribute in the script tag is not required and in fact is non-standardized
Vijay Dev
+5  A: 

First you have to import your file before calling the function using the following

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

Now you can call your function where ever you want.

Gumbo
A: 

What if you have a $(document).ready() in which there is a call to a function defined earlier. Will the fact that it's a document.ready cause it to load or be interpreted before any other imported scripts?

Sam
A: 

I figured out my question. :) The function that's defined in another file needs to be called outside of jQuery and assigned to a variable if you want to use the results inside jQuery. Hope that tidbit helps somebody.

Sam