homepage Welcome to WebmasterWorld Guest from 174.129.163.183
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
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
strem




msg:1478716
 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.

 

joshie76




msg:1478717
 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";
document.body.appendChild(oLink);

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

Josh

tedster




msg:1478718
 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">')
}

strem




msg:1478719
 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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved