Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass parameter to GWT bootstrap .nocache.js script

Tags:

parameters

gwt

Is there any way to pass parameters to the .nocache.js script file generated by GWT and evaluate them in the onModuleLoad function? Like so:

<script type="text/javascript" src="application/Application.nocache.js?appId=461333815262909"></script>

The host page URL should be completely separated from the GWT stuff working inside, so passing the appId parameter as a query parameter for the host page and accessing it with Window.Location.getParameter is not an option. I know that I could hide such parameters e.g. in hidden DIVs and then query them from the script, but if it's possible, I'd love to avoid any further dependency in the host page.

Thanks! Lisa

like image 785
Lisa Avatar asked Dec 04 '10 00:12

Lisa


2 Answers

Instead of hiding information in hidden divs which could get messy, an easy way to pass arguments is via the HTML meta tags.

In the HTML page that calls the GWT script, add a meta tag as follows:

<html>
  <head>
    <meta name="appId" content="461333815262909">
    ...

Then, in your module's entry point, parse it as follows:

@Override
public void onModuleLoad() {
    NodeList<Element> metas = Document.get().getElementsByTagName("meta");
    for (int i=0; i<metas.getLength(); i++) {
        MetaElement meta = (MetaElement) metas.getItem(i);
        if ("appId".equals(meta.getName())) {
            Window.alert("Module loaded with appId: " + meta.getContent());
        }
    }
}

Granted, it's not quite as simple as passing the argument in the src URL of the script tag but I believe it to be a bit cleaner than hiding divs in the document content and less error-prone than artificially re-parsing the script tag's source attribute.

like image 192
Basheer Dargham Avatar answered Nov 12 '22 18:11

Basheer Dargham


No, but this article may be helpful in passing parameters from the server to the client-side script for evaluation on page load.

like image 26
Jason Hall Avatar answered Nov 12 '22 18:11

Jason Hall