PDA

View Full Version : Problem with JavaScript Code - HELP!!!



magicyte
07-03-2008, 05:12 PM
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:



<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

RedZombie125
07-03-2008, 05:24 PM
I went through the code and made a few corrections and came up with this:


<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.

rangana
07-04-2008, 12:55 AM
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:


if((result-64) > 0))


Here's an ammended one:


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.

jscheuer1
07-04-2008, 06:20 AM
Why are you coding for IE 4?!!





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:


var hex = "FFFFFF";

coupled with:


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).

magicyte
07-05-2008, 12:28 AM
As a side note:


var hex = "FFFFFF";

coupled with:


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.

jscheuer1
07-05-2008, 12:33 AM
Why are you using such out of date browsers?!

magicyte
07-05-2008, 01:21 AM
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

jscheuer1
07-05-2008, 01:50 AM
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.

magicyte
07-05-2008, 08:45 PM
Thank you for a complete explaination.

-magicyte