最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calling a JavaScript function inside jQuery - Stack Overflow

programmeradmin1浏览0评论

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
Add a comment  | 

3 Answers 3

Reset to default 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>

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.

发布评论

评论列表(0)

  1. 暂无评论