writing to head using js and dom

D

drewmania001

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/DOM:document.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?
 
M

Martin Honnen

can i create the following...

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

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);
 
D

drewmania001

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!
 
T

Thomas 'PointedEars' Lahn

one comment, i believe "LINK" is a keyword

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
 

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,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top