External javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

External javascript

Hi guys when tried putting test.js at the last line of the body, the alert box was shown first instead of the page. Can someone help me with this issue? Much thanks

13th Mar 2019, 9:40 AM
Tan Fu Yu
Tan Fu Yu - avatar
7 Answers
+ 5
//As Gordon said use setTimeout(), it makes alert box pop after 3000 ms(3s) setTimeout(function() { alert("hello test") }, 3000 )
13th Mar 2019, 9:49 AM
Sudarshan Rai
Sudarshan Rai - avatar
+ 4
setTimeout()
13th Mar 2019, 9:47 AM
Gordon
Gordon - avatar
+ 2
//Maneren actually page will not be visible on foreground, we only see alert box, but page actually runs in background, it's not a problem simply is a window loading. You can do like this to make alert box load when page is fully loaded window.onload = function(){ alert('Hello world '); }
13th Mar 2019, 2:42 PM
Sudarshan Rai
Sudarshan Rai - avatar
13th Mar 2019, 9:50 AM
Tan Fu Yu
Tan Fu Yu - avatar
0
Gordon Shudarshan Rai 👑 Just, can you tell my why this happens? Is it because of asynchronous execution of code or why?
13th Mar 2019, 2:10 PM
Maneren
Maneren - avatar
0
Shudarshan Rai 👑 But i still see alert box and then page. What if i want to make alert box pop up after page shows (without setTimeout for better precision).
13th Mar 2019, 4:25 PM
Maneren
Maneren - avatar
0
Also when i do changeHTML(); //like edit text or something alert("alert"); alert is shown before HTML.
13th Mar 2019, 4:28 PM
Maneren
Maneren - avatar