简体   繁体   中英

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.

How can I do this?

Finally I found a solution! Someone in ZK's Forum gives these possible solutions:

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

lang-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:

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:

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. For example,

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

"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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