Hi I have a page with a lot of .js files, those js files are cause some delays when my page load, I want a code that load those js files after 2 seconds when my page has been 100% loaded, Can anybody help please?
I believe there is a sleep() function, or something similar in php that you can use. <? sleep(2); ?> If you put this at the end of your code, followed by the javascript. It will delay the javascript from running for 2 seconds.
<html> <head> </head> <body> lalalala <script src="x.js"/> </body> </html> Code (markup): Simply put it at the end of the HTML code - it will then be loaded after everything else without the two second delay.
Ohh really???? i was not know lol I already know it lol, but its not working with my website content, i need to add some delay anyway...
There are better ways (putting it at the end of the file like the previous user suggested, or using domcontentloaded and what not) but here you go: <html> <head> <script type="text/javascript"> function loadJavascript(source) { var head= document.getElementsByTagName('head')[0]; var script= document.createElement('script'); script.type= 'text/javascript'; script.src= source; head.appendChild(script); } function loadJavascripts() { loadJavascript('1.js'); loadJavascript('2.js'); alert('loaded'); } window.onload = function() { setTimeout("loadJavascripts()", 2000); } </script> </head> <body> Sup dudes </body> </html> Code (markup): Btw, why would you even want to wait 2 seconds after your page has loaded? That means js functions won't work if the user activates them before that time..
What this code will do? and how i can set the time? and how i can remove that amazing alert? The Reason i want to add delay is that the internet speed is low in my country (512KB/sec for my visitors) so the browser will load those js files even before the page load is complete. I have tested it many times by many browsers, but noway...i should add the delay to fix the problem. The general problem is that some part of the page remain disarranged until the browser load my js files in the end of the page, and then back to the up of the page to load the other js files to arrange the page. the js that i use in the top of the page, is for site arrangement, and the js that i use in the end of the page is for visitor tracking.
The code does what you asked for.. Can't you just look at the code and see the alert line? Just remove it... As for adjusting the time, the "2000" strings is 2000 miliseconds, so convert that to whatever you like. Anyway, there doesn't even need to be a delay. If everything is loaded then you can just add the js, it doesn't need to wait 2 seconds first (waisted time)
Sure, if you live in US or somewhere in europe and your internet speed is 12 Mbps/sec. If you use a slowly connection just for one time in your life i am sure you can feel and know how the page is start loading, its dos not matter if the code was in the end of the page, for the slowly connections it will be loaded even the page is not fully loaded first. It's wasted time when your connection is higher than 3Mpbs/sec, but for here we don't have wasted time in the internet at all. BTW i am not a programmer to know how to remove the alert from your script. i can see something called "alert" in your code, but usually i don't touch something i am not sure what is it .
If your internet speed is so bad, put the desired code to the top of your page, in between the head tags. <head>.....</head> Code (markup): Putting it up there means loading it as fast as possible. If you start to load it 2 seconds after the page load starts, which is what you asked for, your scripts will be loaded two seconds after the time they would have been normally loaded when on top of the page. You cannot load anything faster or earlier than the stuff that is on top of the page. Simply put your code up there, that's as fast as it will get with your slow internet.