Hi. How can I make the following JavaScript which adds a stylesheet (for Internet Explorer) work fine on Netscape 6 and Opera? I really can't get it to work on these browsers.
if (document.createStyleSheet) { //IE ONLY
with (document.createStyleSheet()) { //create stylesheet
addRule("P","font-family: verdana; font-size: 10pt; color: red"); //add rule
}
}
else if (document.getElementsByTagName && document.createElement) { //NS6, OPERA
var head = document.getElementsByTagName("head")[0]; //locate <head>
//create <style>
var mystyle = document.createElement("style");
mystyle.type = "text/css";
mystyle = head.appendChild(mystyle);
//how do I add the rule??
}
adding/modifying css with javascript
It all depends on what effect you're going for by designing for specific browsers.
What browser bug are you trying to get around? There might be a much easier way of doing that than a page preloader javascript CSS modifier
-Mike
adding/modifying css with javascript
I can't do it in another way, because it depends on another script. So, is there any way to add a CSS rule in Netscape and Opera?
adding/modifying css with javascript
Did you ever get an answer to your problem? I have a very similar need. I am creating objects on the fly via scripts and each object must have it's own Style set in order to controlled and when the page is created I will have no idea of how many objects I will have so I can't create 'stub' styles and later change the values in order move them around the page. I need to create a new style to match the object I am creating on-the-fly. And ideally it needs to work on only the newest versions of IE, Netscape, Opera and FireFox. I have been looking on the NET and I have not yet found any good Javascript functions to add new styles to a styleSheet.
Any ideas anyone?
Jim
adding/modifying css with javascript
Did you try:
var myStyle = "<"+"style type=\"text\/css\"" myStyle += ">p {font-family:verdana} < myStyle += "\/style>" var head = document.getElementsByTagName("head")[0] head.addAdjacentHTML("beforeEnd",myStyle):?: