quarks
quarks

Reputation: 35276

Javascript placement in GWT HTML

What is the difference of placing a Javascript (.js) library on before the GWT application nocache.js and after nocache.js in a GWT application which uses the JS library in JSNI methods.

Does it affect wether a JSNI method will be functional or not?

Upvotes: 1

Views: 56

Answers (3)

Tom Carchrae
Tom Carchrae

Reputation: 6486

That really depends on when the JSNI method is loaded.

To be safe - always check to see if the JS lib you are using has been loaded. eg

public native static boolean isJqueryMethodLoaded(String method) /*-{
  if ($wnd.jQuery && $wnd.jQuery[method]) {
      return true;
   } else {
      return false;
   }
}-*/;

(edited - thanks to the other answers for clarifying the load sequence)

Upvotes: 2

Andrea Boscolo
Andrea Boscolo

Reputation: 3048

Normal <script> tag are blocking, but the GWT script tag is not, since it is fetched asynchronously (i.e., non-blocking). The onModuleLoad() is called only when the body has been parsed (i.e., scripts have been fetched). Hence:

  • put it as the very first script in your list: you will save time;
  • <script> tags will always be loaded when onModuleLoad() is called.

Have a look at here.

Upvotes: 1

Thomas Broyer
Thomas Broyer

Reputation: 64541

onModuleLoad is always called at or after DOMContentLoaded, and this will always happen after your scripts have loaded (because they could do document.write()), unless you loaded them with async or defer.

So, unless you load your "other JS" with async or defer, it really shouldn't matter in which order you load them, as onModuleLoad should always be called after both are loaded.

Upvotes: 1

Related Questions