使用ZK:如何将脚本添加到java的head标签中?

时间:2015-03-17 15:07:48

标签: java javascript header zk

使用ZK,我正在尝试以编程方式将标记添加到标头标记中。

我该怎么做?

1 个答案:

答案 0 :(得分:4)

最后我找到了解决方案! ZK论坛的某些人提供了这些可能的解决方案:

http://forum.zkoss.org/question/96845/using-zk-5-how-to-add-a-script-into-the-head-tag-from-java/

“我知道两种方法:

1.在lang-addon.xml中声明javascript文件

琅addon.xml

<?xml version="1.0" encoding="UTF-8"?>

<language-addon>

. . .

<!-- 4. Path to Bootstrap javascript library -->
<javascript src="~./cyborg/less/bootstrap/js/bootstrap.min.js" type="text/javascript" charset="UTF-8" /> 

</language-addon>

<强> 2。在java代码中添加手册:

if (view instanceof Window) {

        Window win = (Window) view;
        PageCtrl pc = (PageCtrl) win.getPage();
        pc.addBeforeHeadTags("<script type=\"text/javascript\">" + "(function(i,s,o,g,r,a,m)"
                + "{i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){"
                + "(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();" + "a=s.createElement(o),"
                + "m=s.getElementsByTagName(o)[0];" + "a.async=1;a.src=g;m.parentNode.insertBefore(a,m)" + "})"
                + "(window,document,'script','//www.google-analytics.com/analytics.js','ga');" + "ga('create', "
                + this.trackingID + ", 'auto'); " + "ga('send', 'pageview');" + "</script>");

    } else {
        throw new UiException("This view model must be applied from a Window component.");
    }
来自api的

void org.zkoss.zk.ui.sys.PageCtrl.addBeforeHeadTags(String tags)

添加将在head元素内和ZK的默认标记之前生成的标记。例如,

((PageCtrl)page).addBeforeHeadTags("");