将参数传递给GWT引导.nocache.js脚本 [英] Pass parameter to GWT bootstrap .nocache.js script

查看:153
本文介绍了将参数传递给GWT引导.nocache.js脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以将参数传递给由GWT生成的.nocache.js脚本文件,并在onModuleLoad函数中对它们进行评估?像这样:

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>

主机页面URL应该与GWT内部完全分开,因此将appId参数传递为主机页面的查询参数并使用Window.Location.getParameter访问它不是一个选项。我知道我可以隐藏这些参数,例如在隐藏的DIV中,然后从脚本中查询它们,但是如果可能的话,我希望避免主机页面上的任何进一步的依赖。

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.

谢谢!
Lisa

Thanks! Lisa

推荐答案

传递参数的简单方法不是将信息隐藏在可能变得混乱的隐藏div中,而是通过HTML元标记。

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

在调用GWT脚本的HTML页面中,添加元标记,如下所示:

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

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

然后,在你的模块的入口点,解析它如下: / p>

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());
        }
    }
}

当然,简单的做法是将参数传递到脚本标记的src URL中,但我相信它比在文档内容中隐藏div更干净,并且比人为地重新解析脚本标记的源属性的错误要少。

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.

这篇关于将参数传递给GWT引导.nocache.js脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆