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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With