I took a look at this is-javascript-location-href-call-is-asynchronous and this what the heck is the event loop anyway but there is no explanation of the defined behavior of href
of window.location
:
The following is the script order in source code:
DOM elements
<script>
at the bottom havingdoc.ready
.Further
<script>
tags.doc.ready
's callback goes into the CB queue because all DOM elements are available in step 1 and there are only further script tags ( This callback stays in the queue until all the scripts in step 3 complete its execution and empties the call stack. Correct? )window.location.href=/error
inside thefail()
of sync AJAX. It does not immediately redirect. So it is async.
// synchronous wait method ~ >= 0.5sec
inside this fail()
method just after (for testing) [TAG: 1]
- call stack becomes empty here as there no more scripts to exec
Question: If href
also acts like Events/XHR/Timeouts, does href
at step 5
go into the event queue? If so, exactly when ? ( Events, AJAX, setTimeouts have defined behavior of when it gets into the queue but what about href
redirection? )
Experiment: If the synchronous wait is there after step 5
, then href
redirection happens "most" of the times. But "sometimes" doc.ready
's callback is also executed.
Question: If href
acts like Event/XHR/Timeouts, then it is always the second in the queue after doc.ready
and so even with the synchronous wait, it should always execute after doc.ready
. But it did not based on the above experiment!! So it shows href
does not involve event queues, right? Then, what is the defined behavior of href
when other Events/XHR/Timeouts are around and relate to Call Stack or Event/CB Queue ?