0
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    abc
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="Scripts/first.js"></script>
    <script src="Scripts/second.js"></script>
</body>
</html>

//first.js
$(document).ready(function () {
    alert(first.firstChild.firstGrandChild);
})

//second.js
var first = {
    firstChild: {
        firstGrandChild: false
    }
}

I have simplified the problem here. We have html page that has reference to two js-first and second. So i assume browser would render html first, stop to download first js, then download second js.

Then document ready of first js would be executed which will get first.firstChild.firstChildChild of second js.

I have few questions: What would be execution order of html execution, download js, document ready execution of fist js, document ready execution of second js (if it has doc ready)?

Now i am able to access these objects from first js but in very similar scenario in my production app, i get these as undefined sporadically. What could be possible reason for that?

If i have circular dependency b/w first and second js and i want things to functions smoothly, what should i do? (example fist js calling second js methods from document ready and from outsid doc ready too AND vice-versa)

Sahil Sharma
  • 3,847
  • 6
  • 48
  • 98
  • document.ready should not trigger until all has loaded - it does not trigger when the JS has loaded – mplungjan Dec 16 '16 at 10:41
  • You need to provide a code that replicates the problem. Posting working code doesn't help. – dfsq Dec 16 '16 at 10:44
  • is there any possibility this execution would fail i.e. first js gives error like first in not defined? Sorry but i am not able to replicate that again. – Sahil Sharma Dec 16 '16 at 10:46
  • It fails for you sometimes? - then yes, there is a possibility. But it's hard to help. Use proper modules, don't use global variables, it would make code more predictable and probably help with this issue too. – dfsq Dec 16 '16 at 10:49
  • Possible duplicate of [JavaScript: DOM load events, execution sequence, and $(document).ready()](http://stackoverflow.com/questions/1307929/javascript-dom-load-events-execution-sequence-and-document-ready) – Simon West Dec 16 '16 at 10:50

0 Answers0