tag, then create the link and apply the attributes. When it's all set up we insert the new cssNode into the head section of our webpage where the various styles are instantly applied. A complete reference for adding, creating, altering and deleting stylesheets and their elements can be found in a newer article titled Totally Pwn CSS with Javascript.
go to top
 
TrackBack URL:
http://rockingjava.blogspot.com/2009/03/hi-friends-this-is-my-first-post.html
2 comments on "dynamic css by javascript"
  Commented by  Dennis Aries, Software and Database Architect, Reflecta    | 04 10 2009 12:03:58 +0000
Nice article, too bad your alignment got lost.

I can see the usefulness of having to load a stylesheet dynamicly like Radhakrishna suggests, but what would be the use in dynamically changing your stylesheet? Let's face it, most websites are pretty static in their styles and if you can alter it (albeit slightly), you will only need to store the user-preference somewhere and either build your stylesheet before sending it to the user, or selecting a specific one from you repository. There are better tools for creating a richer user-experience (I think, but I am not a web-developer myself. :) )

The majority of the users still use IE, so you would be stuck with the 'uncool' method.
  Commented by  Radhakrishna Marar, Business Analyst, Oracle    | 04 10 2009 11:49:12 +0000
Rating : +1 
Welcome Arash,

As JSON and DHTML start to get pushed more and more to the forefront of web-based applications, the web designer is faced with a new problem: how to dynamically insert a script element into an existing web page. It won't take long to figure out that ajax loads and innerHTML injections won't work. 

Dynamic Cascading Style Sheets

The usefulness of being able to dynamically load a style sheet is fairly limited, but there is one very good reason to keep this tool handy: it lets you load a specific stylesheet for a specific browser. Instead of having one massive style sheet for every browser which visits your page, you can break out the stylesheets into browser specific Firefox, IE, Opera, etc styles which accommodate the eccentricities of each browser and let you serve smaller css files to your visitors to boot.

The code for this is just as simple as the javascript.

var headID = document.getElementsByTagName("head")[0];         
var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.href = 'FireFox.css';
cssNode.media = 'screen';
headID.appendChild(cssNode);

We get the <head> tag, then create the link and apply the attributes. When it's all set up we insert the new cssNode into the head section of our webpage where the various styles are instantly applied.

A complete reference for adding, creating, altering and deleting stylesheets and their elements can be found in a newer article titled Totally Pwn CSS with Javascript. 
Add your comment on "dynamic css by javascript"

Rate:
Submit
India's First CRISIL Rated HR Company
  • Create a confidential Career Profile and Resume/C.V. online
  • Get advice for planning their career and for marketing of experience and skills
  • Maximize awareness of and access to the best career opportunities
Register for free today
Viewers also viewed
knowledge vs information
 
772 referals 7 arguments, 76 views
Web Developers vs Web Designers
 
492 referals 15 arguments, 276 views
"HARD WORKERS" vs "NORMAL WORKERS"
 
2 referals 10 arguments, 67 views
more...  
Recent Knowledge (181)
  For most email users, using an email Spam filter to get rid of Spam is the only viable...
1014 referals 6 comments, 95 views
Taking on Facebook, internet search giant Google Wednesday unveiled its version of a social...
 
100 referals 6 comments, 92 views
  Hide Facebook from your boss by converting it into an Excel sheet ! Melbourne, July 06 (ANI):...
 
89 referals 5 comments, 32 views
more...  
More From Author
because they don't want
each project had got a primary goal, in toostep the sharing knowledge is important, however toostep has some problem still, such as commens word-wrapping, editing comments, and Also heavy JQuery-ajax pages, it's okay to having many futures here, but...
I don't know, but I think it doesn't matter
more...  
Build your professional network on facebook via our app Go to app
 
<< Prev  2 of 2 in Topic 
 
Industry : Internet Functional Area : Programming Languages
Activity:  2 comments  480 views  last activity : 07 06 2010 20:18:04 +0000
Share
 
 
 

HI friends this is my first post
Alright today we want talk about change a CSS rule dynamically via javascript
ok we have 2 way to do it
1:an non-standard way,(i mean not cool for a developers)
2:Strong & standard way (but not work in any IE version)



first way:
OK lets start for first way
in first way we should count <a> tags in document and then change the each style attribute one by one
so suppose that our default document has a CSS rule with this structure

<style type="text/css">
a{font-family:monospace;color:#0099dd;}
</style>

&& now we want change the each <a> color tags to something like "#ee1166"
Alright we can use this js function to do it
check this out

function changeA(){
var t=document.getElementsByTagName('a').length;
for(var i=0;i<parseInt(t);i++){
document.getElementsByTagName('a')[i].style.color="#11cc22";
}
}

in code
at the first search for <a> elements and count them then in a for loop change the color of each one

So we have a bad problem here and what is this?

OK suppose that we will have to add some new <a> to document (create <a> dynamic)
OK now we can do it with this guy

function addA(){
var elem = document.createElement('a');
var br=document.createElement("br");
elem.innerHTML="We Well Rock you";
document.getElementById('link').appendChild(elem);
document.getElementById('link').appendChild(br);
}


now if you use both of the functions in a document you will see have a big problem

and now put them all together

so for understanding copy this code in a blank HTML document and run it with your browser (such as FF)

<!--
ArAsh.D by NetBeans 6.5
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>bad a change</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
a{font-family:monospace;color:#0099dd;}
</style>
<script type="text/javascript" >
function changeA(){
var t=document.getElementsByTagName('a').length;
for(var i=0;i<parseInt(t);i++){
document.getElementsByTagName('a')[i].style.color="#11cc22";
}
}
function addA(){
var elem = document.createElement('a');
var br=document.createElement("br");
elem.innerHTML="We Well Rock you";
document.getElementById('link').appendChild(elem);
document.getElementById('link').appendChild(br);
}
</script>
</head>
<body>
<div>
<span>this is a normal text and<br/><a> this is a link</a><br/>
so we want change the 'a's tags color<br/>
so now we ahve a lot of 'a' here:<br/>
<span id="link">
<a>Slipnot</a><br/>
<a>Machine head</a><br/>
<a>linkin park</a><br/>
<a>evanecensce</a><br/>
<a>AC/DC</a><br/>
<a>elvis</a><br/></span><br/>
so now press the "change" button to change the color of 'a's to another
AND press the "add link" button for add new link to the document
</span><br/>
<input type="button" value="change" onclick="changeA()" /><br/>
<input type="button" value="add" onclick="addA()" />
</div>
</body>
</html>


Alright you saw that we have bad problem here and this is
when you click on change you see the change but after the add a link you see that new link has old CSS style color

OK you saw that this method not cool

Now lets start with second way to change CSS rule
2:) cool way
NOTE: this method can not work in IE6-7, so please forget IE and download FireFox or Google chrome

Alright so look a bit in the fundamental of both way and extra features in way no.2
in first way we count a tags and change each tag color, it means our CSS rule has old rule still and in add new tags browser use of old rules

in second way: OK look when you load a page, it means that CSS rules is in your system (client-side) so you can access to it and change the rules of base

OK
like previous system we have a CSS rule like this

<style type="text/css">
a{font-family:monospace;color:#0099dd;}
</style>


and now we have method for change the CSS rule (no each <a> tag rule)

this is

function changeCSS(){
document.styleSheets[0].cssRules[0].style.color="#991177";
}


OK I should say something here
first note: look at this part

document.styleSheets[0]

styleSheets[x]
why do we should use pointer [x] here?
because maybe we have 3 or 4 or x style sheet in our document
for example

<link rel="stylesheet" type="text/css" rel=nofollow href="CSS1.css" /> <!--for access to this style sheet we should use document.styleSheets[0]-->
<link rel="stylesheet" type="text/css" rel=nofollow href="CSS2.css" /> <!--for access to this style sheet we should use document.styleSheets[1]-->
<link rel="stylesheet" type="text/css" rel=nofollow href="CSS3.css" /> <!--for access to this style sheet we should use document.styleSheets[2]-->
<link rel="stylesheet" type="text/css" rel=nofollow href="CSS4.css" /> <!--for access to this style sheet we should use document.styleSheets[3]-->

NOTE: maybe we use of some style sheets with out link them to our document such as

<style type="text/css">
a{font-family:monospace;color:#0099dd;}
</style>

so for this session we have this

<head>
<link rel="stylesheet" type="text/css" rel=nofollow href="CSS1.css" /> <!--for access to this style sheet we should use document.styleSheets[0]-->
<link rel="stylesheet" type="text/css" rel=nofollow href="CSS2.css" /> <!--for access to this style sheet we should use document.styleSheets[1]-->
<link rel="stylesheet" type="text/css" rel=nofollow href="CSS3.css" /> <!--for access to this style sheet we should use document.styleSheets[2]-->
<style type="text/css"> <!--for access to this style sheet we should use document.styleSheets[3]-->
a{font-family:monospace;color:#0099dd;}
</style>
<link rel="stylesheet" type="text/css" rel=nofollow href="CSS4.css" /> <!--for access to this style sheet we should use document.styleSheets[4]-->
</head>

so as you see reference and point the step by step
second Note: look at this part

document.styleSheets[0].cssRules[0]

cssRules[x]
you see we have use pointer here too because we CAN have more than 1 rule in each sheet
so with this example you will understand this guy

<style type="text/css">
b{font-family:tahoma;color:black;} <!--for access to this rule we should use document.styleSheets[0].cssRules[0]-->
code{font-family:monospace;color:#333333;} <!--for access to this rule we should use document.styleSheets[0].cssRules[1]-->
</style>
<style type="text/css">
a{font-family:Arial;color:#0099dd;} <!--for access to this rule we should use document.styleSheets[1].cssRules[0]-->
</style>


OK put them all together with a add function for add new <a> tag

<!--
ArAsh.D by NetBeans 6.5
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>cool change</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
a{font-family:monospace;color:#0099dd;}
</style>
<script type="text/javascript" >
function addA(){
var elem = document.createElement('a');
var br=document.createElement("br");
elem.innerHTML="We Well Rock you";
document.getElementById('link').appendChild(elem);
document.getElementById('link').appendChild(br);
}
function changeCSS(){
document.styleSheets[0].cssRules[0].style.color="#991177";
}
</script>
</head>
<body>
<div>
<span>this is a normal text and<br/><a> this is a link</a><br/>
so we want change the 'a's tags color<br/>
so now we ahve a lot of 'a' here:<br/>
<span id="link">
<a>Slipnot</a><br/>
<a>Machine head</a><br/>
<a>linkin park</a><br/>
<a>evanecensce</a><br/>
<a>AC/DC</a><br/>
<a>elvis</a><br/></span><br/>
so now press the "change" button to change the color of 'a's to another
AND press the "add link" button for add new link to the document
</span><br/>
<input type="button" value="add" onclick="addA()" /><br/>
<input type="button" value="change CSS" onclick="changeCSS()" />
</div>
</body>
</html>



So please see both ways and difference
and you have to do it and something like that for understand for ever

if you have any question ask me!
good luck mi amigos

 Top Comment : Radhakrishna Marar   | 04 10 2009 11:49:12 +0000
Welcome Arash, As JSON and DHTML start to get pushed more and more to the forefront of web-based applications, the web designer is faced with a new problem: how to dynamically insert a script element into an existing web page. It won't take long to figure out that ajax loads and innerHTML injections won't work. Dynamic Cascading Style Sheets The usefulness of being able to dynamically load a style sheet is fairly limited, but there is one very good reason to keep this tool handy: it lets you load a specific stylesheet for a specific browser. Instead of having one massive style sheet for every browser which visits your page, you can break out the stylesheets into browser specific Firefox, IE, Opera, etc styles which accommodate the eccentricities of each browser and let you serve smaller css files to your visitors to boot. The code for this is just as simple as the javascript. var headID = document.getElementsByTagName("head")[0]; var cssNode = document.createElement('link'); cssNode.type = 'text/css'; cssNode.rel = 'stylesheet'; cssNode.href = 'FireFox.css'; cssNode.media = 'screen'; headID.appendChild(cssNode); We get the