Can Javascript add a new style class ?

P

pamelafluente

Hi guys,

Is it possible to add "onload" (via Javascript) a new class to the
<style> header section?
If yes, how would that be done ?

<style type="text/css" media="screen">
.NewStyleClass{ whatever }
</style>


-P
 
A

agapeton

You need to state the purpose and end result of what you are doing.
Perhaps there's a right way to do it that's not a complete hack.
 
S

scriptguru

(e-mail address removed) напиÑав:
Hi guys,

Is it possible to add "onload" (via Javascript) a new class to the
<style> header section?
If yes, how would that be done ?

<style type="text/css" media="screen">
.NewStyleClass{ whatever }
</style>


-P
what about document.write()? (But not with onload)
or document.createElement("STYLE") (with onload)?
 
P

pamelafluente

Thanks guys, I going to check those suggestions.

Is it going to work with both MSIE and FF (that's the minimum
compatibility I need) ?

The purpose is simply that I want to add some css/html
dinamically from javascript. It's kind of optional code that
I need to add "onload" only under some circumstances.


-P
 
A

ASM

(e-mail address removed) a écrit :
The purpose is simply that I want to add some css/html
dinamically from javascript. It's kind of optional code that
I need to add "onload" only under some circumstances.

<html>
<script type="text/javascript">

function newClass(class, attributes) {
var S = document.createElement('style');
S.type = 'text/css';
var T = '.'+class+' { '+attributes+' }';
T = document.createTextNode(T)
S.appendChild(T);
document.body.appendChild(S);
}

</script>

<style type="text/css">
p { color: blue; cursor: pointer; font-size: 2em }
</style>

<p
onclick="newClass('pizza','color:lime;background:#F33;text-align:center');
this.className=this.className==''?'pizza':'';">
voici un petit essai de pizza<br>
little test : pizza ?<br>
poi ? pizza ?
</p>
</html>



Don't know if that works with IE
 
P

Peter Michaux

Thanks guys, I going to check those suggestions.

Is it going to work with both MSIE and FF (that's the minimum
compatibility I need) ?

The purpose is simply that I want to add some css/html
dinamically from javascript. It's kind of optional code that
I need to add "onload" only under some circumstances.

Why not have the various possible CSS options already statically
waiting the stylesheet and then add the correct class to the body
element to activate whatever you want? Using decendent selectors this
is very flexible.

Here is an example

http://evil.che.lu/

Peter
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top