Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Style Tag to Body Tag

Status
Not open for further replies.

Signit

MIS
Oct 17, 2003
114
0
0
US
On some pages I have reason for adding a watermark, but those pages will not always require said watermark. As such I would like to dynamically add a style attribute to the body tag with JavaScript. I've written the below code to add the style attribute to a page's body tag, but I don't get my watermark. If I place the style attribute directly in the body tag I get the appropriate result. What am I missing here?

My body tag looks like this:
Code:
// initializeScreen() has code to call addReadOnlyWatermark()
<BODY BGCOLOR="cornsilk" onLoad="initializeScreen();">

Code:
function addReadOnlyWatermark()
{ 
  var bodyTag = document.getElementsByTagName("body")[0];
  bodyTag.setAttribute("style","background-image:url(../images/readonly.gif);background-attachment:fixed;background-position:100% 100%;background-repeat:no-repeat;");
}
 
Maybe
Code:
<BODY BGCOLOR="cornsilk" onLoad="addReadOnlyWatermark()">
 
After some more digging I found the answer.

Code:
function addReadOnlyWatermark()
{ 
  var bodyTag = document.getElementsByTagName("body")[0];
  bodyTag.style.backgroundImage = "url(../images/readonly.gif)";
  bodyTag.style.backgroundAttachment = "fixed";
  bodyTag.style.backgroundPosition = "100% 0%";
  bodyTag.style.backgroundRepeat = "no-repeat";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top