Computer Science Canada

The Prompt Command.

Author:  fear01 [ Wed Jan 18, 2006 5:10 pm ]
Post subject:  The Prompt Command.

Sorry for posting again, but this question is really bugging me and is the only thing standing in my way of my work. I was wondering how I could use the prompt command to change the color of the text.

I get the user's name and prompt them again for a color, but whenever the color is input it will either be black or another color that was previously done before. Basically it only works half the time and I want to know how to change this. (Ex. What is your name? ____. What color text do you want? ____. = NAME but wrong or no color.)

Author:  md [ Wed Jan 18, 2006 5:31 pm ]
Post subject: 

I'm confused as to what this has to do with xhtml, unless you are doing this for a webpage...

Author:  fear01 [ Wed Jan 18, 2006 5:46 pm ]
Post subject: 

I am! I am! Come back!

Author:  md [ Wed Jan 18, 2006 6:17 pm ]
Post subject: 

do you have a link to this page? It's hard to help debug things when I've got no idea how you are doing something

Author:  fear01 [ Wed Jan 18, 2006 6:30 pm ]
Post subject: 

I don't know how to do the code thing, so i just pated it.


<HTML>

<HEAD>
<title></title>
<META NAME="author" CONTENT="">
</HEAD>

<body>
<body bgcolor="red">

<script language="JavaScript">
var user_name;
var userpic;
user_name = prompt("What is your name?", "");
user_pic1 = prompt("What text color would you like?", "");

document.write("<center> <font color='user_pic1'> Welcome ", user_name, " to the world of JavaScript" </font color>

</center>);
</script>

</body>

</html>

I have been doing this for a week and stuck on how to change the font color. Go ahead and laugh at my lame skills, but I'll get better, then conquer the world...

Author:  md [ Wed Jan 18, 2006 6:57 pm ]
Post subject: 

First, I'll rewite your stuff using xhtml instead of HTML which is on it's way out. Second, you might want to read about CSS, as it's what you probably want to be using.

code:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN'
   'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head>
    <title>You should really have a title! ;)</title>
    <meta name="author" content="cornflake">
</head>

<body>
<body style='background-color: RGB(255,0,0)'>
 
    <script language="JavaScript">
        var user_name;
        var userpic; 
        user_name = prompt("What is your name?", ""); 
        user_pic1 = prompt("What text color would you like?", "");
        if(user_pic1 == "red")
            rgb_color = "RGB(255,0,0)"
        if(user_pic1 == "green")
            rgb_color = "RGB(0,255,0)"
        if(user_pic1 == "blue")
            rgb_color = "RGB(0,0,255)"
        else
            rgb_color = "RGB(0,0,0)"
 
        document.write("<span style='text-align: center; color: ", rgb_color, "'>Welcome ", user_name, ", to the world of JavaScript.</span>");

    </script>

</body>

</html>

note that you'll have to add to the color code for any other colors you want.

It seems that there is some resistance to using xhtml and CSS here on compsci, but it is a much nicer standard then HTML; and aside from IE (which soon might get support for much of CSS) it's a widly used standard. Plus it makes things so much nicer Razz You can use the w3c validator (http://validator.w3.org) to check to see if your page is valid. IT tells you pretty specifically what you need to fix if it isn't.

Author:  fear01 [ Wed Jan 18, 2006 7:10 pm ]
Post subject: 

I see, I see. But is there anyway to put the variable for color into some sort of script that changes it to the color desired. Or am I going to have to put in the general colors for it to work? By the way, thank you for your help, it has been most appriciated.

Author:  md [ Wed Jan 18, 2006 7:49 pm ]
Post subject: 

you could write a function, but in the end I think you'll end up with a big list of if statements somewhere.

Author:  fear01 [ Wed Jan 18, 2006 8:12 pm ]
Post subject: 

Ah fudge...


: