Connecting Tech Pros Worldwide Help | Site Map

writing to head using js and dom

  #1  
Old May 10th, 2006, 03:45 PM
drewmania001@gmail.com
Guest
 
Posts: n/a
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?

  #2  
Old May 10th, 2006, 04:15 PM
Martin Honnen
Guest
 
Posts: n/a

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/
  #3  
Old May 11th, 2006, 10:45 AM
drewmania001@gmail.com
Guest
 
Posts: n/a

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!

  #4  
Old May 19th, 2006, 12:55 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
to learn jQuery if already using prototype liketofindoutwhy answers 83 June 27th, 2008 08:10 PM
VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help gunimpi answers 0 January 10th, 2007 08:55 PM
Adding Script Element to the DOM Joseph Scoccimaro answers 7 November 23rd, 2005 03:30 AM
** Dynamically writing html/javascript from a javascript function ** Robby Bankston answers 9 July 23rd, 2005 09:55 PM