Results 1 to 9 of 9

Thread: Problem with JavaScript Code - HELP!!!

  1. #1
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default Problem with JavaScript Code - HELP!!!

    Help is urgent!!! I created a JavaScript code the other day, and I have no idea what is wrong with it. When I opened it up in my IE 4 internet browser, there was an error. I double-clicked on the little exclaimation point error thingy in the bottom left-hand corner. A new window opened and said that there was an error on line 36. I don't know what is wrong. Here is the code, though I have only accomplished a little:

    Code:
    <html>
    <head>
    <script language="JavaScript">
    var r = 255;
    var b = 255;
    var g = 255;
    var b1 = 11111111;
    var b2 = 11111111;
    var b3 = 11111111;
    var hex = "FFFFFF";
    
    function b0()
    {
    r = document.getElementById('r1').value;
    b = document.getElementById('b1').value;
    g = document.getElementById('g1').value;
    }
    
    function d1()
    {
    var c1 = 1;
    var c2 = 1;
    var c3 = 1;
    var c4 = 1;
    var c5 = 1;
    var c6 = 1;
    var c7 = 1;
    var c8 = 1;
    var result = 255;
    
    if((result-128) > 0)
    {
    c1 = "1";
    result -= 128;
    }
    else
    {
    c1 = "0";
    result = result;
    }
    if((result-64) > 0))
    {
    c2 = "1";
    result -= 64;
    }
    else
    {
    c2 = "0";
    result = result;
    }
    if((result-32) > 0))
    {
    c3 = "1";
    result -= 32;
    }
    else
    {
    c3 = "0";
    result = result;
    }
    if((result-16) > 0))
    {
    c4 = "1";
    result -= 16;
    }
    else
    {
    c4 = "0";
    result = result;
    }
    if((result-8) > 0))
    {
    c5 = "1";
    result -= 8;
    }
    else
    {
    c5 = "0";
    result = result;
    }
    if((result-4) > 0))
    {
    c6 = "1";
    result -= 4;
    }
    else
    {
    c6 = "0";
    result = result;
    }
    if((result-2) > 0))
    {
    c7 = "1";
    result -= 2;
    }
    else
    {
    c7 = "0";
    result = result;
    }
    if((result-1) > 0))
    {
    c8 = "1";
    result -= 1;
    }
    else
    {
    c8 = "0";
    result = result;
    }
    alert(c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8);
    }
    
    function d2()
    {
    }
    
    function d3()
    {
    }
    
    function ch()
    {
    }
    
    function change()
    {
    document.getElementById('bg').style.background=hex;
    }
    </script>
    </head>
    
    <body onload="d1()" id="bg">
    
    <INPUT TYPE="text" MAXLENGTH="6" id=color
    onkeydown="change()" onkeyup="change()">
    
    </body>
    
    </html>
    If anyone could please help me and tell me what is wrong, my burden will be lifted. Very much thanks in advance!!! The only part that is being activated is my problem. It is the function d1().

    -magicyte

  2. #2
    Join Date
    Jul 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I went through the code and made a few corrections and came up with this:

    HTML Code:
    <html>
    <head>
    <script language="JavaScript">
    var r = 255;
    var b = 255;
    var g = 255;
    var b1 = 11111111;
    var b2 = 11111111;
    var b3 = 11111111;
    var hex = "FFFFFF";
    
    function b0()
    {
    r = document.getElementById('r1').value;
    b = document.getElementById('b1').value;
    g = document.getElementById('g1').value;
    }
    
    function d1()
    {
    var c1 = 1;
    var c2 = 1;
    var c3 = 1;
    var c4 = 1;
    var c5 = 1;
    var c6 = 1;
    var c7 = 1;
    var c8 = 1;
    var result = 255;
    
    if (result-128 > 0)
    {
    c1 = "1";
    result -= 128;
    }
    else
    {
    c1 = "0";
    result = result;
    }
    if (result-64 > 0)
    {
    c2 = "1";
    result -= 64;
    }
    else
    {
    c2 = "0";
    result = result;
    }
    if (result-32 > 0)
    {
    c3 = "1";
    result -= 32;
    }
    else
    {
    c3 = "0";
    result = result;
    }
    if (result-16 > 0)
    {
    c4 = "1";
    result -= 16;
    }
    else
    {
    c4 = "0";
    result = result;
    }
    if (result-8 > 0)
    {
    c5 = "1";
    result -= 8;
    }
    else
    {
    c5 = "0";
    result = result;
    }
    if (result-4 > 0)
    {
    c6 = "1";
    result -= 4;
    }
    else
    {
    c6 = "0";
    result = result;
    }
    if (result-2 > 0)
    {
    c7 = "1";
    result -= 2;
    }
    else
    {
    c7 = "0";
    result = result;
    }
    if (result-1 > 0)
    {
    c8 = "1";
    result -= 1;
    }
    else
    {
    c8 = "0";
    result = result;
    }
    alert(c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8);
    }
    
    function d2()
    {
    }
    
    function d3()
    {
    }
    
    function ch()
    {
    }
    
    function change()
    {
    document.getElementById('bg').style.background=hex;
    }
    </script>
    </head>
    
    <body onload="d1()" id="bg">
    
    <INPUT TYPE="text" MAXLENGTH="6" id=color
    onkeydown="change()" onkeyup="change()">
    
    </body>
    
    </html>


    I'm not totally sure what you wanted this to do, but at least it doesn't have problems whenever it's started.
    Last edited by Snookerman; 04-21-2009 at 07:20 AM. Reason: added [html] tags

  3. #3
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    First, language is a deprecated attribute.

    Secondly, you have wrong syntax. There could'nt be multiple else statement, you must use else if instead.

    Lastly, you have an excess closing parenthesis on the highlighted part which goes all the way to all your if statement:
    Code:
    if((result-64) > 0))
    Here's an ammended one:
    Code:
    function d1()
    {
    var c1 = 1,c2 = 1;,c3 = 1,c4 = 1,c5 = 1,c6 = 1,c7 = 1,c8 = 1,result = 255;
    if((result-128) > 0)
    {c1 = "1";result -= 128;}
    else if((result-64) > 0)
    {c2 = "1";result -= 64;}
    else if((result-32) > 0)
    {c3 = "1";result -= 32;}
    if((result-16) > 0)
    {c4 = "1";result -= 16;}
    else if((result-8) > 0)
    {c5 = "1";result -= 8;}
    else if((result-4) > 0)
    {c6 = "1";result -= 4;}
    else if((result-2) > 0)
    {c7 = "1";result -= 2;}
    else if((result-1) > 0)
    {c8 = "1";result -= 1;}
    else
    {c1 ="0",c2="0",c3="0",c4="0",c5="0",c6="0",c7="0",c8="0",result = result;}
    alert(c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8);}
    Hope it helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Why are you coding for IE 4?!!

    Quote Originally Posted by rangana View Post
    Code:
    if((result-64) > 0))
    The above quoted and several like it were the only functional problems with the code. The last closing parenthesis has no corresponding opening parenthesis. However, the entire code seems fairly ill conceived and has inadequate markup in the body section of the page to allow for its full execution.

    As a side note:

    Code:
    var hex = "FFFFFF";
    coupled with:

    Code:
    document.getElementById('bg').style.background=hex;
    will result in invalid style in most browsers (though probably not in IE 4 if it supported document.getElementById, which it doesn't).
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    As a side note:

    Code:
    var hex = "FFFFFF";
    coupled with:

    Code:
    document.getElementById('bg').style.background=hex;
    will result in invalid style in most browsers (though probably not in IE 4 if it supported document.getElementById, which it doesn't).
    Actually, it shouldn't. I have Mozilla 4.0. I am thinking that it is the same to IE.

    -magicyte

    P.S. Thank you all, by the way.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Why are you using such out of date browsers?!
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Well, first of all, I am using IE 6 on this Windows XP, and on my other computer, a Windows ME with IE 4 as the browser, I program. The other computer doesn't have internet, but this one does. I should probably program on this one. By the way, it stinks that I got an infraction from djr33.

    -maigycte

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You should (I'm assuming a Windows based system here, as you've indicated that's what you have) use FF or Opera as your primary browser for live surfing, or even Flock, just about anything but IE or Safari 3 Win (or anything based directly on them), as those later two have serious security issues for your system. They are OK for testing stuff in, even important for that, as many folks use them or similar, in fact Safari 3 Win is about the best way to test some of what many Mac users will see. They are also great for trusted sites that may require their features. But for general surfing and initial testing, protect your computer by using one of the others I mention.

    You should probably upgrade to IE 7 on the XP machine - it can handle it, and it has been around for awhile, 'stand alone' installations of earlier IE's for testing (on the XP machine) can best (as far as I know - it's what I now use after struggling with inferior implementations) be installed using:

    http://tredosoft.com/Multiple_IE

    As far as the infraction goes, we moderators try not to interfere with each other. I know djr33 can be a little (and I stress a little) overprotective of the board at times, but look at it this way:

    His infraction is technically correct. It will expire without incident as long as you don't make a habit of it. You really should strive to have more descriptive titles for your threads. Things like 'help', or 'help with script' don't tell anyone very much. The main ideas are that the more descriptive your title, the more likely you will receive help from someone familiar with and/or interested in the topic - And perhaps more importantly - when others search the forum for help, a good descriptive title will help them find useful information.
    Last edited by jscheuer1; 07-05-2008 at 10:47 PM. Reason: spelling
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Thank you for a complete explaination.

    -magicyte

Bookmarks

Posting Permissions

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