Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT inject CSS on runtime

I'm using gwt 2.3.0 in my project. I need to change my css source:

<link type="text/css" rel="stylesheet" href="gxt/css/gxt-all.css">

during run time (i want to decide which file to use on onModuleLoad method). what is the best wat to do so?

like image 281
Adi Mor Avatar asked Feb 05 '12 07:02

Adi Mor


1 Answers

To inject a CSS file, you need to proceed in a similar way as ScriptInjector does for javascript file :

/** Load CSS file from url */
public static void loadCss(String url){
    LinkElement link = Document.get().createLinkElement();
    link.setRel("stylesheet");
    link.setHref(url);
    nativeAttachToHead(link);
}

/**
 * Attach element to head
 */
protected static native void nativeAttachToHead(JavaScriptObject scriptElement) /*-{
    $doc.getElementsByTagName("head")[0].appendChild(scriptElement);
}-*/;

@jusio:

StyleInjector.inject(...) works with CSS content only :

StyleInjector.inject(".myClass{color:red;}");
like image 76
JBE Avatar answered Sep 21 '22 11:09

JBE