Behaviour of different javascript execution contexts

后端 未结 1 1441
太阳男子
太阳男子 2021-01-29 15:28

There is a popup say - alert(\"Hi Tuhin!\");
What are the basic differences when we use alerts with various methods and events listed below:

  1. Direc
相关标签:
1条回答
  • 2021-01-29 15:46

    Hmm, this is more of a question of how/when these alerts get fired.

    1) This will alert when the script is loaded, ergo whenever your code gets that far down.

    2) Not too sure which one you're trying to call here, either document.observe('dom:loaded', function(){}) which is deprecated or document.addEventListener("DOMContentLoaded", function(event){}). The latter, which I assume is the one you meant, will fire as soon as the DOM is loaded without waiting for any CSS/images to finish.

    3) window.onload will fire after all elements have loaded in the DOM, including images and whatnot.

    4) $("document).ready() fires similarly to DOMContentLoaded, where it will fire as soon as the DOM is loaded.

    5) window.alert() is pretty similar to using just alert(), however calling window.alert() is a good idea if you have a function in your scope called alert.

    0 讨论(0)
提交回复
热议问题