0

Let name it as A.js and B.js.

On page load of an aspx page, I am trying to load JS in the order A.js,B.js.

I used a method in B.js which is declared in A.js. But I am getting an error function undeclared alert in console.

But when I tried alerting either from B.js or A.js, script is working fine. It is taking the method and page is good. Why so?

What I need to do for making it work without alerting?

Praveen Prasannan
  • 6,802
  • 9
  • 45
  • 67

2 Answers2

1

Scripts are loaded asynchronously, so you never know which file will load first. Most probably one with less bytes.

If you want to load scripts in order you provide, use library like requirejs.

Rafał Łużyński
  • 5,889
  • 5
  • 23
  • 35
0

I did a small example and used window.load event which did work for me. Check out this question on SO or jquery.load

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="a.js"></script>
    <script src="b.js"></script>
</head>
<body>
    <script>
        window.addEventListener("load", function () {
            methodB();
        }, false);
    </script>
</body>
</html>
Community
  • 1
  • 1
Pilgerstorfer Franz
  • 8,093
  • 3
  • 39
  • 51