Solved

javascript to change background color

Posted on 2011-09-23
11
212 Views
Last Modified: 2012-05-12
Is there a way to temporarily set a html background color to #000000 until the window has fully loaded and then change it to transparent?
0
Comment
Question by:rgranlund
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 9

Expert Comment

by:user_n
ID: 36588151
0
 
LVL 7

Author Comment

by:rgranlund
ID: 36588215
User N.  That is an interesting example but it does not answer my question.  I want to set a page color until the page completely loads that I want the background color to automatically change.
0
 
LVL 9

Expert Comment

by:user_n
ID: 36588241
onLoad and onUnload

The onLoad and onUnload events are triggered when the user enters or leaves the page.

The onLoad event is often used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information.

Both the onLoad and onUnload events are also often used to deal with cookies that should be set when a user enters or leaves a page. For example, you could have a popup asking for the user's name upon his first arrival to your page. The name is then stored in a cookie. Next time the visitor arrives at your page, you could have another popup saying something like: "Welcome John Doe!".
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 9

Expert Comment

by:user_n
ID: 36588260
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 36588275
I think the .ready() function in jQuery would be the most efficient way to do what you want. Something like this:
...
<style type="text/css">
.body_0 {background-color:#def}
.body_1 {background-color:#000}
  </style>  
      <script src="scripts/jquery-1.6.1.min.js" type="text/javascript"></script>
      <script type="text/javascript">
         $(document).ready(function () {
                  $("body").removeClass('body_1').addClass('body_0');
            });
      </script>
</head>
<body class="body_1">
...

Trying to do it with just javascript looks pretty involved from what I read.

http://stackoverflow.com/questions/799981/document-ready-equivalent-without-jquery
0
 
LVL 9

Expert Comment

by:user_n
ID: 36588302
0
 
LVL 7

Author Comment

by:rgranlund
ID: 36588427
@TommyBoy and et all:

I need to change it here:

<!DOCTYPE html>
<html style="background-color: #000000;">

So I need to use javascript here to change the color from black to transparent after page fully loads.
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 36588598
I would suggest a similar solution:

<!DOCTYPE html>
<html class="html_1">
<head>
 <title>Untitled</title>
 <style type="text/css">
.html_0 {background-color:transparent}
.html_1 {background-color:#000}
 </style>
<script src="scripts/jquery-1.6.1.min.js" type="text/javascript"></script>
      <script type="text/javascript">
      $(document).ready(function () {
             $("html").removeClass('html_1').addClass('html_0');
        });
      </script>

</head>
<body>
</body>
</html>
0
 
LVL 3

Accepted Solution

by:
timhigham earned 250 total points
ID: 36592487
transparent background ?????
Is this the body element you are trying to set or a high z-level mask on top of the whole page?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<script type="text/javascript" src="jscripts/callStats.js"></script>
<style type="text/css">
      .loadBackground{
            background-color:#000000;
            
      }
      .normalBackground{
            background-color:#0000ff;
      }
</style>
<title></title>
</head>
<body id="body" class="loadBackground">

<script type="text/javascript">
// final part of page so the rest has loaded
      alert("test pause");
      document.body.className="normalBackground";
</script>
<!-- NOTHING BELOW HERE EXCEPT CLOSE OF BODY -->
</body>
</html>
0
 
LVL 3

Expert Comment

by:timhigham
ID: 36592492
PS ignore
<script type="text/javascript" src="jscripts/callStats.js"></script>
its irrelevant - thanks to COPY / PASTE
0
 
LVL 7

Author Comment

by:rgranlund
ID: 36599873
I have used this to rectify the issue with Google Chrome flashing white when you load a new screen.  It does not validate however, it works like a charm.  Thanks.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question