4 replies [Last post]
lopardo2003
Offline
newbie
Last seen: 17 years 9 weeks ago
Timezone: GMT-3
Joined: 2003-07-21
Posts: 2
Points: 0

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??

}

DJSdotcom
Offline
Enthusiast
Rochester, NY / Chicago, IL
Last seen: 17 years 27 weeks ago
Rochester, NY / Chicago, IL
Joined: 2003-03-23
Posts: 95
Points: 0

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 Wink

-Mike

</twocents>

lopardo2003
Offline
newbie
Last seen: 17 years 9 weeks ago
Timezone: GMT-3
Joined: 2003-07-21
Posts: 2
Points: 0

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?

jdhunter
Offline
newbie
Last seen: 16 years 6 weeks ago
Timezone: GMT-7
Joined: 2004-08-12
Posts: 1
Points: 0

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

kugelman
Offline
newbie
Last seen: 16 years 4 weeks ago
Timezone: GMT-5
Joined: 2004-08-29
Posts: 1
Points: 0

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)  
:?: