Link to home
Start Free TrialLog in
Avatar of mike99c
mike99c

asked on

Cannot add a style to a body tag using JavaScript if FireFox detected

Hello,
I have a website which has a background tile image. However, I need to change the background tile image if the browser is Firefox.

Hence for IE the body tag is:
<body>

But for Firefox it must be:
<body style='background-image:url(/images/common/main_bg.gif)'>

I have attached a code snippet in which I successfully use JavaScript to detect if the browser is Firefox. However, when I view the site in Firefox the whole page simply displays:
style='background-image:url(/images/common/main_bg.gif)'

and that's it, just that line of code and nothing else. It works fine in IE.



<script type="text/javascript">
function checkfirefox(){
	if (navigator.userAgent.indexOf("Firefox")!=-1)
	document.write("style='background-image:url(/images/common/main_bg.gif)'")
}
</script>
<body onload="checkfirefox()">

Open in new window

Avatar of silemone
silemone
Flag of United States of America image

try using this script or any other standard browser javascript to see if it works...

if not, then there may be an error with your styles.
also,

looking at the script code you're writing:

try:

 document.body.style.background =

finally what errors are you getting...is the path correct?
Avatar of mike99c
mike99c

ASKER

In response to the 3 preceding reponses:

1. I have checked the styles by manually embedding them and ite work. So I have ruled out any issue with the style.

2. I have looked at the quirksmode link but I have no issue detecting the browser, it is simply embedding the css that is the problem.

3. I have checked the path by embedding the style so that is not the issue either.
you're embedding style, but for what element?  what i mean is where is that code going to be placed?  that's why I said explicitedly set


2:
3:
4:
5:
6:
7:

      

<script type="text/javascript">
function checkfirefox(){
        if (navigator.userAgent.indexOf("Firefox")!=-1)
       {
               document.body.style.background = url(/images/common/main_bg.gif);
        }
}
</script>
<body onload="checkfirefox()">
ASKER CERTIFIED SOLUTION
Avatar of mike99c
mike99c

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial