Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > CSS loading using JS

Reply
Thread Tools

CSS loading using JS

 
 
Brett Foster
Guest
Posts: n/a
 
      02-24-2005
Is there a way to load CSS at runtime using JS in IE and Mozilla (and
optionally Opera)? I've been able to do this with scripts by using the DOM.

I'm trying to stage loading up certain components of a page ONLY when
they are required to improve initial load times.

Brett
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      02-24-2005


Brett Foster wrote:

> Is there a way to load CSS at runtime using JS in IE and Mozilla (and
> optionally Opera)? I've been able to do this with scripts by using the DOM.


Create a <link> element with the desired properties and insert it into
the head of the document e.g.
var link;
if (document.createElement && (link = document.createElement('link')))
{
link.href = 'whatever.css';
link.rel = 'stylesheet';
link.type = 'text/css';
var head = document.getElementsByTagName('head')[0];
if (head) {
head.appendChild(link);
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
optimistx
Guest
Posts: n/a
 
      02-24-2005
Martin Honnen wrote:
>
>
> Brett Foster wrote:
>
>> Is there a way to load CSS at runtime using JS in IE and Mozilla (and
>> optionally Opera)? I've been able to do this with scripts by using the
>> DOM.

>
>
> Create a <link> element with the desired properties and insert it into
> the head of the document e.g.
> var link;
> if (document.createElement && (link = document.createElement('link')))
> {
> link.href = 'whatever.css';
> link.rel = 'stylesheet';
> link.type = 'text/css';
> var head = document.getElementsByTagName('head')[0];
> if (head) {
> head.appendChild(link);
> }
> }
>

Why not in the head-section:

document.write('<link rel="stylesheet" type="text\/css" href="w.css">');

Less characters, easy to understand and remember . But if there is a
reason to avoid this , I listen carefully.
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      02-24-2005


optimistx wrote:


> Why not in the head-section:
>
> document.write('<link rel="stylesheet" type="text\/css" href="w.css">');


I think the original poster wants to be able to load a stylesheet with
script after the page has been loaded and then document.write doesn't help.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
Brett Foster
Guest
Posts: n/a
 
      02-25-2005
Martin Honnen wrote:
>
>
> Brett Foster wrote:
>
>> Is there a way to load CSS at runtime using JS in IE and Mozilla (and
>> optionally Opera)? I've been able to do this with scripts by using the
>> DOM.

>
>
> Create a <link> element with the desired properties and insert it into
> the head of the document e.g.


Mozilla recognizes the style sheet has been inserted, but no styles seem
to have been loaded/applied.

> var link;
> if (document.createElement && (link = document.createElement('link')))
> {
> link.href = 'whatever.css';
> link.rel = 'stylesheet';
> link.type = 'text/css';
> var head = document.getElementsByTagName('head')[0];
> if (head) {
> head.appendChild(link);
> }
> }
>

 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      02-25-2005


Brett Foster wrote:

> Martin Honnen wrote:
>


>> Create a <link> element with the desired properties and insert it into
>> the head of the document e.g.

>
>
> Mozilla recognizes the style sheet has been inserted, but no styles seem
> to have been loaded/applied.
>
>> var link;
>> if (document.createElement && (link = document.createElement('link')))
>> {
>> link.href = 'whatever.css';
>> link.rel = 'stylesheet';
>> link.type = 'text/css';
>> var head = document.getElementsByTagName('head')[0];
>> if (head) {
>> head.appendChild(link);
>> }
>> }


I have code similar to the above in an example here:
<http://home.arcor.de/martin.honnen/javascript/200502/test2005022501.html>
there with both Firefox 1.0 and Netscape 7.2 the stylesheets are loaded
and the CSS is applied.

If you still have problem, then show us exactly what you are doing,
which Mozilla version you use, how the CSS looks, hopefully reduced to a
simple test case.


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP + JS + CSS: why ajax doesn't display using CSS on main doc? SeanInSeattle Javascript 5 12-13-2007 12:50 PM
order of iframe loading with document loading ofir Javascript 0 12-03-2007 12:06 PM
loading image -> detect when image is done loading edfialk Javascript 0 05-10-2007 07:28 PM
[OT] Is loading the second Java application faster than loading the first? David Segall Java 2 01-02-2007 04:41 PM
Image loading using javascript. Handling timeouts and parrallel loading under IE zborisau@gmail.com Javascript 4 08-28-2005 02:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57