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

mike99cAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

silemoneCommented:
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.
0
silemoneCommented:
0
silemoneCommented:
also,

looking at the script code you're writing:

try:

 document.body.style.background =

finally what errors are you getting...is the path correct?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

mike99cAuthor Commented:
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.
0
silemoneCommented:
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()">
0
mike99cAuthor Commented:
Hello,
I am found a solution but have come up with a different approach.
I have placed the style change in a separate style sheet and used JavaScript to only include the style sheet for Firefox.
I have attached the code snipet.
<script type="text/javascript">
	if (navigator.userAgent.indexOf("Firefox")!=-1)
	document.write("<link rel='stylesheet' href='/css/sitestyles-firefox.css' type='text/css' media='all' />")
</script>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.