Executing JS function without <body onload="...">

Discussion in 'JavaScript' started by dodox, Jun 30, 2007.

  1. #1
    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 ...
     
    dodox, Jun 30, 2007 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    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):
     
    Logic Ali, Jun 30, 2007 IP
  3. veckd

    veckd Peon

    Messages:
    1,065
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i would put it directly after the <body> section so it loads first.
     
    veckd, Jun 30, 2007 IP
  4. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #4
    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/
     
    ansi, Jun 30, 2007 IP
  5. dodox

    dodox Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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 :(
     
    dodox, Jul 1, 2007 IP
  6. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #6
    then look into the addEvent() funtions floating around like i mentioned before. with those you add as many events onload as you like.
     
    ansi, Jul 1, 2007 IP