SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    bgColor does not work in Firefox, is there an alternative?

    I tried this in my script:
    Code:
    newWindow.document.bgColor="#CC9900";
    It does not work in FF, is there an alternative for adding color directly to the document object?


    Thanks guys!


    Novice

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,433
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    Where did you get that from? I'd be surprized if it works in any browser!

    I'm guessing you're guessing? Not always a bad thing, I do it quite often myself and its often faster than taking the time to look up the correct syntax.

    Anyway try ..... [elem name goes here].style.backgroundColor =

    The js syntax is different than the CSS, pascalCase instead of hyphens i.e.
    backgroundColor instead of background-color

  3. #3
    SitePoint Zealot coothead's Avatar
    Join Date
    Jan 2005
    Location
    Chertsey, London, England
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there Novice2010,

    document.bgColor="#CC9900" does work in Firefox and all other browsers that I tested in.

    So your problem must lay elsewhere in your code.

    This is my test code...
    Code:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <title>document.bgColor test</title>
    
    <script type="text/javascript">
    
    function init(){
       document.bgColor='#c90';
     }
    
     window.addEventListener?
     window.addEventListener('load',init,false):
     window.attachEvent('onload',init);
    
    </script>
    
    </head>
    <body>
    <div></div>
    </body>
    </html>
    
    coothead

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Where did you get that from? I'd be surprized if it works in any browser!

    I'm guessing you're guessing? Not always a bad thing, I do it quite often myself and its often faster than taking the time to look up the correct syntax.

    Anyway try ..... [elem name goes here].style.backgroundColor =

    The js syntax is different than the CSS, pascalCase instead of hyphens i.e.
    backgroundColor instead of background-color
    I am not guessing.

    I am reading the following book:

    Beginnning JavaScript 4th Edition.

    By Paul Wilton and Jeremy McPeak.

    Page 285, Chapter 8.

    "If you wanted to change the background color of the document contained inside the new window, you would type this."
    Code:
    newWindow.document.bgColor = "red";
    I am trying to add a background color to a pop up window when a user click okay on the prompt box.

    Here is the code, the background color changes in IE for the pop up window but not in FF.

    Code:
    function promptBox(){
        
         var message = prompt("Who is your favorite Hollywood Star? ", "");
         if (message == "")
         message = "Come on! That was an easy question. You should have answered it.";
         myWindow = window.open('','','width=800,height=600');
         myWindow.document.write(message);
    	 myWindow.document.bgColor="#666666";
         myWindow.focus();
    }
    Maybe I misunderstood, not sure.

    Thanks for pointing this out!


    So why would this be in the book?


    Novice.

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coothead View Post
    Hi there Novice2010,

    document.bgColor="#CC9900" does work in Firefox and all other browsers that I tested in.

    So your problem must lay elsewhere in your code.

    This is my test code...
    Code:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <title>document.bgColor test</title>
    
    <script type="text/javascript">
    
    function init(){
       document.bgColor='#c90';
     }
    
     window.addEventListener?
     window.addEventListener('load',init,false):
     window.attachEvent('onload',init);
    
    </script>
    
    </head>
    <body>
    <div></div>
    </body>
    </html>
    
    coothead
    You are right! This is what I have noticed, this particular color
    Code:
    #fbf3eb";
    does not work at all.

    Do you know why?


    Novice

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,433
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    Hmmm, I've never seen bgColor before. I learn something new here all the time.
    I apologize if I came across seeming a bit harsh

    I'm wondering if using bgColor is a "web safe color" type of thing. i.e. only works with 16,16,16 colors but not 256,256,256 colors?

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Hmmm, I've never seen bgColor before. I learn something new here all the time.
    I apologize if I came across seeming a bit harsh

    I'm wondering if using bgColor is a "web safe color" type of thing. i.e. only works with 16,16,16 colors but not 256,256,256 colors?
    Please, do not apologize. I am a novice, I can take a little of that.

    I have a massive collection of JavaScript books. The problem is most of these books will have 12 champters deveted to theory and 1 chapter to actual practical JavaScript. LOL!!

    Any, it see all other colors will work but when I try this:
    Code:
    #fbf3eb";
    However this would work:
    Code:
    #9CF000
    .

    I am confused as well.

    Is there another way I can add background color to the pop up window?


    IC

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,598
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Hmmm, I've never seen bgColor before. I learn something new here all the time.
    In this case you have learnt something ancient.

    bgColor was the way it was done before CSS was introduced.

    If you set background-color in your CSS you can't override it using bgColour. The only way to get bgColor to work is if you use HTML 3.2 and set bgcolor= inside your HTML.

    The need for bgColour should have completely disappeared by about 2004 or so and so is not something important to learn unless you need to know about it in order to know how to rewrite the code for the 21st Century.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •