External stylesheet setting in Firefox

B

Barry

Hello. I am in the process of modifying some JavaScript code which
refers to an external stylesheet, but unfortunately I have very little
previous JavaScript experience. Currently the code has only been
written to be used in IE but I am looking into getting it to work for
Firefox. I have addeda small piece of code to detect Firefox and then
wish to reset the stylesheet from the IE version to a Firefox version.
Unfortunately the same line of code which works in IE does not work in
Firefox:

document.styleSheets[0].href = "styles/FIREFOX/" + stylesheet;

which gives the following error in the debugger:

Error: setting a property that has only a getter

This is quite a vague error and I cannot figure out an alternative. As
I understand it, the href property is read-only in Firefox so is there
some other method I can use instead?

Thanks in advance.
 
J

josh

Barry ha scritto:
document.styleSheets[0].href = "styles/FIREFOX/" + stylesheet;
Error: setting a property that has only a getter

in W3c/Firefox that is only a read-only property but
you can dinamically change the href as follow:

i.e.

your style:
<link id ="my_style" rel="stylesheet" type="text/css"
href="../css/ui_controls.css"/>

your function:
function changeStyle(css)
{
var l = document.getElementById("my_style");
l.href = css;
}

than with an event pass your css url file to that function:
<input type="button" value="change style"
onclick="changeStyle('other.css')"/>

Bye
 
V

vynogradov

If you have multiple stylesheets like this:

<link rel="stylesheet" href="one.css">
<link rel="stylesheet" href="two.css">
<link rel="stylesheet" href="three.css">

you can activate them in javascript with

document.styleSheets.disabled = true;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top