4

I am using JavaScript and jQuery. My main file has My.js and Ajax.

My.js

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

My main file

<script type="text/javascript">
 ..
 // Here I want to make call function defined in My.js build_one()
 ..
 // Here is the Ajax call
 $.ajax({
 type:'POST',
 url: 'ajax.php',
 data:'id='+id ,
 success: function(data){
 $("#response").html(data);
 }
 });
 ...
</script>

How do I make the build_one() function call before the Ajax function?

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
asked Jan 9, 2009 at 13:54
1
  • Am I missing something in the question ? Looking at the couple of answers, I think not. I am really amazed at the simplicity of the question, coming from someone using jQuery Commented Jan 9, 2009 at 14:28

3 Answers 3

9

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>
Oded
501k102 gold badges900 silver badges1k bronze badges
answered Jan 9, 2009 at 14:03
Sign up to request clarification or add additional context in comments.

2 Comments

You beat me to it. I was going to say the exact same thing. Even the code is the same.
Using language attribute in the script tag is not required and in fact is non-standardized
5

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.

answered Jan 9, 2009 at 14:04

Comments

2

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.

answered Aug 6, 2010 at 20:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.