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
venkatachalam
102k31 gold badges75 silver badges77 bronze badges
-
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 jQueryVijay Dev– Vijay Dev2009年01月09日 14:28:09 +00:00Commented Jan 9, 2009 at 14:28
3 Answers 3
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>
Sign up to request clarification or add additional context in comments.
2 Comments
Sarel Botha
You beat me to it. I was going to say the exact same thing. Even the code is the same.
Vijay Dev
Using language attribute in the script tag is not required and in fact is non-standardized
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
Gumbo
657k112 gold badges792 silver badges852 bronze badges
Comments
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.
Comments
lang-js