Connecting Tech Pros Worldwide Forums | Help | Site Map

writing to head using js and dom

drewmania001@gmail.com
Guest
 
Posts: n/a
#1: May 10 '06
Hi everyone!

i've been reading up on access to the dom via js (i'm new to this dom
stuff)

i understand tutorials and info such as
http://developer.mozilla.org/en/docs....createElement

my problem: i'd like js to create the code for a stylesheet in the head
of the docuement!

i'm not sure how do do this (or if it can be done)

can i create the following...

<link rel="stylesheet" type="text/css"
href="mystyle.css" />

OR

<style type="text/css">
hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("images/back40.gif")}
</style>

using the dom or anything else in js alone?...or am i fighting a losing
battle.

(what i really want at the end of the day is to detect the browser and
use a css file according to that file, either with js or php)

any suggestions or pointers?


Martin Honnen
Guest
 
Posts: n/a
#2: May 10 '06

re: writing to head using js and dom




drewmania001@gmail.com wrote:

[color=blue]
> can i create the following...
>
> <link rel="stylesheet" type="text/css"
> href="mystyle.css" />[/color]

Yes, create the link element as follows
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'mystyle.css';
then insert as needed
document.getElementsByTagName('head')[0].appendChild(link);



--

Martin Honnen
http://JavaScript.FAQTs.com/
drewmania001@gmail.com
Guest
 
Posts: n/a
#3: May 11 '06

re: writing to head using js and dom


WOW it worked!!! (not that i didnt have faith in you or anything)/

one comment, i believe "LINK" is a keyword so i just changed the var to
mylink and vala sucess!

thanks so much i had the first part down pat it was
getElementsByTagName and using the array to append the child i needed
help with.

once again thankyou, thankyou, thankyou...now i can move on with my
life!

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#4: May 19 '06

re: writing to head using js and dom


drewmania001@gmail.com wrote:
[color=blue]
> one comment, i believe "LINK" is a keyword[/color]

You believe wrong. However, it is not a bad idea to avoid such
ambiguities in order to avoid problems in case a broken script
engine/AOM/DOM interferes.

Please quote the minimum of what you are replying to next time.


PointedEars
--
When the power of love overcomes the love
of power, the world will know peace.
-- Jimi Hendrix
Closed Thread