简体   繁体   English

使用ZK:如何从Java将脚本添加到head标签?

[英]Using ZK: How to add a script into the head tag from java?

Using ZK, I'm trying to add a script into the header tag programmatically. 使用ZK,我试图以编程方式将脚本添加到标头标记中。

How can I do this? 我怎样才能做到这一点?

Finally I found a solution! 终于我找到了解决方案! Someone in ZK's Forum gives these possible solutions: ZK论坛中的某人提供了以下可能的解决方案:

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

"I know two ways for that: “我知道两种方式:

1.Put the declarartion of the javascript file in the lang-addon.xml 1.将javascript文件的声明放入lang-addon.xml中

lang-addon.xml 郎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. Add manual in java code: 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.");
    }

from api: 从api:

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

Adds the tags that will be generated inside the head element and before ZK's default tags. 在ZK的默认标签之前添加将在head元素内部生成的标签。 For example, 例如,

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

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM