Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8010213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:40:13+00:00 2026-06-04T18:40:13+00:00

Running this code in browser, if no colour is entered, ‘green'(undefined) is rendered. Random/garbage

  • 0

Running this code in browser, if no colour is entered, ‘green'(undefined) is rendered.

Random/garbage value leads to black background…
…not selecting or transparent gives white(null)

<HTML>
<HEAD>
<TITLE>Document Object Demo</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=Javascript>
var sAnswer = window.prompt("Please enter your favourite color : choose from the following Yellow, Green, Red, Blue, Pink, Orange, Black, Gray")
document.bgColor = sAnswer
document. write ("<H1>Welcome " + sAnswer + ":-)!!</H1><HR>We are very happy to demonstrate the usage of <I> document. write</I>. Do remember to view the source!<P>")
</SCRIPT>
</BODY>
</HTML>

I use IE9…!

Why is this happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:40:14+00:00Added an answer on June 4, 2026 at 6:40 pm

    You are having this problem because the browser will try to parse something out of w/e you set the bgColor to…

    Now your Javascript is fairly questionable, and I can not think of a reason you’d want to do this, but your code should probably look more like this.

    (function () {
        // Prompts the user to give an answer, this prevents further code execution
        var sAnswer = window.prompt("Please enter your favourite color : choose from the following Yellow, Green, Red, Blue, Pink, Orange, Black, Gray");
    
        // Takes care of all falsy valuyes, 0, null, undefined, false
        // Ideally some more validation on the input should be done
        if(sAnswer) {
            document.body.style.backgroundColor = sAnswer;
            document.write ("<h1>Welcome " + sAnswer + ":-)!!</h1><hr><p>We are very happy to demonstrate the usage of <i> document. write</i>. Do remember to view the source!</p>");
        } else {
            document.write ("<p>You did not fill in a valid answer! Refresh.</p>");
        }
    }());
    

    OR keep asking the question until a valid answer is given.. (fairly evil imo)

    (function () {
        var sAnswer;
    
        do {
            sAnswer = window.prompt("Please enter your favourite color : choose from the following Yellow, Green, Red, Blue, Pink, Orange, Black, Gray")
        } while ( !sAnswer );
    
        document.body.style.backgroundColor = sAnswer;
        document.write ("<h1>Welcome " + sAnswer + ":-)!!</h1><hr><p>We are very happy to demonstrate the usage of <i> document. write</i>. Do remember to view the source!</p>");
    
    }());
    

    Code not tested but fairly confident it should work :p

    Edit, how does it get to the colour green?

    In short, undefined is typecasted to a string which can be used to set the bgColor property, which in turn is parsed and interpreted as a color, making invalid hex characters count for 0. Read this for a more in depth explanation.

    Internet Explorer used this interpretation and the other browsers followed so to not break backwards compatibility.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running this code, and although the web browser appears, the web
Running this code from Herb Sutter's presentation . This works fine in linux under
running this code: NSLog(@%f, MKMetersBetweenMapPoints(MKMapPointMake(33.6523, -118.507), MKMapPointMake(34.516, -117.628))); returns this: 0.015819 When the expected
Im running this code in my website to get the photos on a set,
I am running this code and getting unexpected results. I expect that the loop
I have a problem running this code in firefox. It's ok in chrome but
While I am running this code it showing me a following error:- Column 'virtuemart_product_id'
I'm running this code in Firefox 11 on Windows 7. (See http://jsfiddle.net/QStkd/ ). $('<div><input
Hi I am having an error running this code Map<String, Integer> map = new
I get a segmentation fault when running this code. Anyone know why? Thanks. #include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.