When does the order of functions make sense?

I understand that JS pre-compiles functions before executing the code. Therefore, the order of the functions does not matter. But the order of the functions somehow becomes a problem when linking * .js files.

For instance,

<script src="@Url.Content("~/Scripts/Personal/MyJScript.js")" type="text/javascript"></script>


<script type="text/javascript">
    $(document).ready(function () {
        hello();
        afterCall();
        hello2(); //fails, defined in MyJScript2.js

    });

    function afterCall() {
        alert('inline function defined after call');
    }
</script>

<script src="@Url.Content("~/Scripts/Personal/MyJScript2.js")" type="text/javascript"></script>

In the above code, the function hello2()is defined in the file that is linked after the call is defined. Call failed. So, intuitively, I assume that now the order of the functions matters in this case.

Given what I am doing $(document).ready, the document should be as complete as it is. So why is this happening?


As requested, here is the client side of HTML
<body>
    <script src="/Scripts/Personal/MyJScript.js" type="text/javascript"></script>


<script type="text/javascript">
    $(document).ready(function () {
        hello();
        afterCall();
        hello2(); //fails

    });

    function afterCall() {
        alert('inline function defined after call');
    }
</script>

<script src="/Scripts/Personal/MyJScript2.js" type="text/javascript"></script>

</body>
+5
source share
3 answers

, , $(document).ready js-, .

, DOM , HTTP- , , script .

, , , , /: http://havenshade.com/tmp/testalert.html

: (

+1

script , , ( ) , JS.

, .

0

Instead of using the script tag to import MyJscript2, you can use getScript to capture the script and perform some functions in the success callback.

0
source

All Articles