www.webdeveloper.com
Results 1 to 5 of 5

Thread: Javascript not working in IE?

  1. #1
    Join Date
    Dec 2010
    Posts
    3

    Javascript not working in IE?

    My code isn't working in IE, can anyone tell me why?

    ---------------------------------------------------------------


    <html><body>
    <span id="minutes"> 0 </span> min
    <span id="seconds"> 0 </span>. <span id="mseconds"> 0 </span> sec
    <script type="text/javascript">
    var tmseconds = 20*60*10;
    var mseconds;
    var minutes;
    var hours;
    var days;
    function countdown()
    {
    mseconds = --tmseconds;
    minutes = tmseconds /(60*10);
    minutes = Math.floor(minutes);
    tmseconds = tmseconds - (minutes * 60*10);
    seconds = tmseconds /10;
    seconds = Math.floor(seconds);
    tmseconds = tmseconds - (seconds*10);
    tmseconds = Math.floor(tmseconds);
    document.getElementById('minutes').innerHTML = minutes;
    document.getElementById('seconds').innerHTML = seconds;
    document.getElementById('mseconds').innerHTML = tmseconds;
    tmseconds = mseconds;
    if(tmseconds >0){
    ID=setTimeout("countdown();", 100);}
    }
    window.onload = countdown();
    </script>
    </body></html>

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    A scope conflict is occurring: The id seconds and the variable seconds in the function are both global in scope. Use the keyword var in the function to make all variables local to the function.
    https://developer.mozilla.org/en/Jav...Variable_Scope
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Dec 2010
    Posts
    3

    not working still

    can you please give me an example, i did what you suggested i thought. but i must be missing something.

  4. #4
    Join Date
    Mar 2009
    Posts
    495
    I am not in a place now where I have access to a machine with IE explorer on it, otherwise I would have tested it, but here is something you might try. Normally, when you set up 'window.onload = ...' and there is just one function such as 'countdown' like you have you normally don't put the parenthesis after the function name, because that means the result of the function rather than the function itself is fired, which can produce an error if that's not what you want.

    Your code does work in FF, but I suspect its because the result of your countdown function sets a timeout that invokes the function itself. Anyway, perhaps you might try changing:
    Code:
     window.onload = countdown();
    to
    Code:
    window.onload = countdown;
    I don't know if this will work or not, but it won't take much effort to test it.

  5. #5
    Join Date
    Dec 2010
    Posts
    3
    Thanks for the help here is the solution that works:

    <html><body>


    <SPAN id="minutes"> 0 </SPAN> min
    <SPAN id="seconds"> 0 </SPAN>. <SPAN id="mseconds"> 0 </SPAN> sec
    <script LANGUAGE="JavaScript">

    var tmseconds = 20*60*10;
    var mseconds;
    var minutes;
    var hours;
    var days;


    function countdown()
    {
    var mseconds = --tmseconds;


    var minutes = tmseconds /(60*10);
    minutes = Math.floor(minutes);
    tmseconds = tmseconds - (minutes * 60*10);

    var seconds = tmseconds /10;
    seconds = Math.floor(seconds);
    tmseconds = tmseconds - (seconds*10);

    tmseconds = Math.floor(tmseconds);


    document.getElementById('minutes').innerHTML = minutes;
    document.getElementById('seconds').innerHTML = seconds;
    document.getElementById('mseconds').innerHTML = tmseconds;

    tmseconds = mseconds;
    if(tmseconds >0){
    setTimeout("countdown();", 100);}
    }

    window.onload = countdown();
    </script>
    </body></html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles