0

How to write this code in Javascript

How to write this Jquery code in Javascript and that code may do the same thing like in Jquery. https://code.sololearn.com/WyBnz8TKUD7X/?ref=app

24th Jun 2017, 6:30 AM
Nipun Shihara
Nipun Shihara - avatar
2 Answers
+ 3
Real strict exactly same behaviour of JQuert(document).ready() is done through use of addEventListener() method with 'DOMcontentLoaded' event: document.addEventListener('DOMcontentLoaded',function); ... where 'function' is a function name or an inlined function declaration (as the commonly anonymized function passed in that kind of case): document.addEventListener('DOMcontentLoaded',function() { setTimeout(function() { document.getElementById ("demo").innerHTML =""; for (i = 1; i < 6; i++) { document.getElementById("demo").innerHTML += i+'<br>'; } }, 2000); }); @Calvin (lazy) way is equivalent, but event is not fired to same moment: + 'load' event occurs at very end of loaded Html and all linked ressources as images, external css and js... + 'DOMcontentload' occurs without waiting for external ressources, as soon as Html is parsed, so DOM (Document Object Model) is accessible through javascript On slow connexions (low bandwidth) and/or heavy external ressources, ellapsed time between them could be signifiant, but mostly of times the 'lazy' way is enough, even if 'proper' (modern) way to dynamically attach event to element would be by using the addEventListener() method (old way of accessing event attribute of element cannot handle assignement of many listeners and work only with oldest events -- there's no corresponding 'onDOMcontentLoaded' attribute in 'document' object ^^)
24th Jun 2017, 7:12 AM
visph
visph - avatar
0
change $(document).ready( {code} ); with window.onload = {code}
24th Jun 2017, 6:49 AM
CalviŐČ
CalviŐČ - avatar