DeadPassive
DeadPassive

Reputation: 887

GWT - Loading browser-specific JavaScript

I've got a JavaScript fix specifically for IE9 and want to load it into my project only for that browser. I thought I might be able to do something like this in my .gwt.xml:

<script src="ie9svgfix.js">
    <when-property-is name="user.agent" value="ie9"/>
</script>

But unfortunately this doesn't work. Does anybody know a clean way to do this in GWT?

Thanks,

Jon

Upvotes: 1

Views: 179

Answers (2)

Thomas Broyer
Thomas Broyer

Reputation: 64541

The cleanest way in GWT would be to use deferred-binding and inject the script with the ScriptInjector in the IE9 permutation; or have the script loaded by the host page, in which case you can use conditional comments (as suggested by Stano).

With deferred-binding, you'd have to create a class to "deferred-bind" with a specific implementation for IE9.

class SvgFix {
  public void fix() { /* no op */ }
}

class SvgFixIE9 {
  @Override
  public void fix() {
    ScriptInjector.fromUrl(GWT.getModuleBaseForStaticFiles() + "ie9svgfix.js")
        .setWindow(ScriptInjector.TOP_WINDOW)
        .inject();
  }
}

And in your EntryPoint, inject the script:

GWT.<SvgFix>create(SvgFix.class).fix();

And finally then choose the appropriate implementation based on permutation:

<replace-with class="com.example.client.SvgFixIE9">
  <when-type-assignable class="com.example.client.SvgFix" />
  <when-property-is name="user.agent" value="ie9" />
</replace-with>

BTW, note that <script> in gwt.xml files is not supported with the xsiframe linker, and I'd encourage you to use it going forward (it has all the advantages of all the other linkers, and none of their drawbacks, plus it adds Super Dev Mode, flexibility/configurability, etc.)

Upvotes: 3

Stano
Stano

Reputation: 8939

You can try conditional comments:

<!--[if IE 9]>
<script src="ie9svgfix.js"></script>
<![endif]-->

Upvotes: 3

Related Questions