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?
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?
Share Improve this question edited Apr 20, 2013 at 6:33 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jan 9, 2009 at 13:54 venkatachalamvenkatachalam 102k31 gold badges74 silver badges77 bronze badges 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 – Vijay Dev Commented Jan 9, 2009 at 14:28
3 Answers
Reset to default 9This 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>
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.
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.