homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Dynamically load a css from js
js load css

 9:03 am on Apr 18, 2003 (gmt 0)

I am new to js so this question may seam dumb and the answer obvious, but can anybody tell me how can I load dynamicaly a css from js.



 9:41 am on Apr 18, 2003 (gmt 0)

Using the DOM of course:

var oLink = document.createElement("link")
oLink.href = "mypath/mycss.css";
oLink.rel = "stylesheet";
oLink.type = "text/css";

This will only work in DOM compatible browsers like Opera 7+, IE5+, and NN6+.



 9:42 am on Apr 18, 2003 (gmt 0)

You can also use document.write() to add the appropriate text. Here's a simple script I've called from the <head> to give Netscape 4 it's own stylesheet, before I learned about the @import hack:

if (document.layers) {
document.write('<link rel=stylesheet href="nn4.css">')
else {
document.write('<link rel=stylesheet href="main.css">')


 2:57 pm on Apr 18, 2003 (gmt 0)

Thanks! That was really helpful.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved