How to dynamically load css style in GWT-based application

1,720 views
Skip to first unread message

yadong

unread,
Dec 12, 2007, 4:22:59 AM12/12/07
to Google Web Toolkit
I am working on an web application in which the css style varies based
on different user, I am curious how to load css file dynamically.
Currently we use JSTL tag to access server side parameter to determine
which css file needs to be used.

Robert Hanson

unread,
Dec 12, 2007, 8:03:04 AM12/12/07
to Google-We...@googlegroups.com
> Currently we use JSTL tag to access server side parameter to determine
> which css file needs to be used.

If you can still do the same thing I would, it is the best/easiest way to do it.

If your use-case requires that it be loaded dynamically, you could use
some JSNI.

private native void loadCSS (String cssHref) /*-{
document.write('<link rel="stylesheet" type="text/css" href="' +
cssHref + '">');
}-*/;


Rob
http://roberthanson.name

Charlie Collins

unread,
Dec 12, 2007, 9:15:08 AM12/12/07
to Google Web Toolkit
Search for the same requirement on this forum, this question comes up
often and there are a lot of answers/techniques. The Rocket framework
also has a CSS switcher in it (http://code.google.com/p/rocket-gwt/).

Yuri C

unread,
Jan 15, 2013, 1:52:57 PM1/15/13
to google-we...@googlegroups.com, Google Web Toolkit
I have 2 CSS loaders.
One relies on <link> tags. This is the preferred way to load CSS as long as you don't need to be notified when the stylesheet is loaded.
Another one updates the page with CSS loaded via a separate HTTP request and you can register a callback to be notified when the CSS is loaded.
CssLinkLoader.java
CssStyleLoader.java

Benjamin Cuthbert

unread,
Sep 2, 2014, 3:56:56 AM9/2/14
to google-we...@googlegroups.com, Google-We...@googlegroups.com
Hi Yuri,

How do you make the call to the server? via the onModuleLoad?

Yuri C

unread,
Sep 2, 2014, 11:31:02 AM9/2/14
to google-we...@googlegroups.com, Google-We...@googlegroups.com
In my case the idea was to have a CSS per screen. I call the server the first time the very first time that particular "screen" is revealed.

Benjamin Cuthbert

unread,
Sep 3, 2014, 2:54:15 AM9/3/14
to google-we...@googlegroups.com, Google-We...@googlegroups.com
Okay interesting. So do you have the css deployed into the war? Or unpacked on the server?

Ltearno

unread,
Apr 9, 2015, 9:38:05 AM4/9/15
to google-we...@googlegroups.com, Google-We...@googlegroups.com
Hi !

The tool HexaCss for GWT answers exactly your needs (as i understand them).

With it you can produce several CSS files for the same GWT application (and still using them in a type-safe way, like with CssResource) and you can then change them dynamically without reloading the application.

Here is the link to the tool (open source on github)

I hope this helps you !

Thanks
Arnaud
Reply all
Reply to author
Forward
0 new messages