Hi, I need help from us I have this JS file: var a, b, xtime, clock, minute, second = 0; var xlocation, m = ""; function initTimer(time, location) { m = document.getElementById("d2"); i = 125; xlocation = location; xtime = time; clock = time; //document.getElementById("d1").innerHTML=""; m.style.width=0; clearTimeout(a); clearTimeout(b); startCount(); startProgressBar(); } function startProgressBar() { if (i >= 0) { if (clock < 10) { m.style.background="red"; } else { m.style.background="blue"; } m.style.width=i+"px"; a = setTimeout("startProgressBar();", xtime/125*1000); } i--; } function startCount() { if (clock != 0) { minute = Math.floor(clock/60); second = (clock-minute*60); b = setTimeout('startCount();', 1000); } else { window.location.href = xlocation; } clock --; showClock(); } function showClock() { minute = minute.toString(); second = second.toString(); if (minute.length < 2) { minute = "0" + minute; } if (second.length < 2) { second = "0" + second; } document.getElementById("clock").innerHTML = minute + ":" + second; } Basicly, I need execute initTimer(time, location) function everytime If I refresh my browser, but I can´t use onload method...for example... <body onload="initTimer(180, 'http://www.example.com');"> Is here somebody, who can solve that ? Thanks a lot ...
You could insert the following code within the <body> section, at any point below all the elements addressed by the script. If you are uncertain, insert it immediately before the </body> tag. <script type='text/javascript'> initTimer(180, 'http://www.example.com'); </script> Code (markup):
there's a couple other methods as well. window.onload = function(){ //do onload } or if you want to call multiple events onload check out the addevent and removeevent functions floating around or write your own. an example: http://ejohn.org/projects/flexible-javascript-events/
addition: This JS generating clock (countdown) with progress bar (div with 125px width which is getting smaller with counted time). But I have problem, because I use lightbox script in the same page. And it doesn´ t work properly if I call both script. (I discover, problem is caused by the bod=document.getElementsByTagName('body')[0]; in lightbox script). If I delete lightbox (or command above), it works properly. But I need all of them
then look into the addEvent() funtions floating around like i mentioned before. with those you add as many events onload as you like.