开发者

Pass parameter to GWT bootstrap .nocache.js script

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 a开发者_Go百科ccessing 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


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.


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


There appears to be no native support in GWT for that, but I came up with the following solution lately:

Assuming that your script always follows the naming convention "/<moduleName>.nocache.js", you can fetch all <script> elements from the host page and search for the one which references this in the src attribute. You can then pull the URL-encoded attributes from there.

Here's my sample implementation, meant to be called with GWT.getModuleName() as the first parameter.

/**
 * Fetches a parameter passed to the module's nocache script.
 * 
 * @param moduleName the module's name.
 * @param parameterName the name of the parameter to fetch.
 * @return the value of the parameter, or <code>null</code> if it was not
 *   found.
 */
public static native String getParameter( String moduleName, String parameterName ) /*-{
    var search = "/" + moduleName + ".nocache.js";
    var scripts = $doc.getElementsByTagName( "script" );
    for( var i = 0; i < scripts.length; ++i ) {
        if( scripts[ i ].src != null && scripts[ i ].src.indexOf( search ) != -1 ) {
            var parameters = scripts[ i ].src.match(/\w+=\w+/g);
            for( var j = 0; j < parameters.length; ++j ) {
                var keyvalue = parameters[ j ].split( "=" );
                if( keyvalue.length == 2 && keyvalue[ 0 ] == parameterName ) {
                    return unescape( keyvalue[ 1 ] );
                }
            }
        } 
    }
    return null;
}-*/;   

Suggestions for improvement are welcome.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜