Package org.zkoss.zul

Class Script

All Implemented Interfaces:
Serializable, Cloneable, Component, Scope, ComponentCtrl

public class Script extends AbstractComponent
A component to generate script codes that will be evaluated at the client. It is similar to HTML SCRIPT tag, except the defer option (setDefer(boolean)) will cause the evaluation of JavaScript until the widget has been instantiated and mounted to the DOM tree.

Note: it is the script codes running at the client, not at the server. Don't confuse it with the zscript element.

There are several way to embed script codes in a ZUML page:

Approach 1: Specify the URL of the JS file without defer. The JavaScript codes are evaluated as soon as the file is loaded.

<script src="my.js"/>
 

Approach 2: Specify the JavaScript codes directly without defer. The JavaScript codes are evaluated immediately before the widget is instantiated, so you cannot access any widget. Rather, it is used to do desktop-level initialization, such as defining a widget class, and a global function.

<script defer="true">
 zk.$package('foo');
 zk.load('zul.wgt', function () {
 foo.Foo = zk.$extends(zul.Widget, {
 //...
</script>
 

Approach 3: Specify the JavaScript codes directly with defer. The JavaScipt codes are evaluated after the widget is instantiated and mounted. Moreover, this references to the script widget, so you can access the widgets as follows.

<script defer="true">
 this.getFellow('l').setValue('new value');
 //...
</script>
 

Alternative to Script, you can use the script directive as shown below..

<?script src="/js/mine.js"?/>
 <?script content="jq.IE6_ALPHAFIX=/.png/"?/>
 
Author:
tomyeh
See Also:
  • Constructor Details

    • Script

      public Script()
    • Script

      public Script(String content)
      Constructs a script component with the specified content.
      Parameters:
      content - the content (the code snippet).
      Since:
      6.0.0
  • Method Details

    • getCharset

      public String getCharset()
      Returns the character encoding of the source. It is used with getSrc().

      Default: null.

    • setCharset

      public void setCharset(String charset)
      Sets the character encoding of the source. It is used with setSrc(java.lang.String).

      Refer to HTML Character Setsfor more information.

    • getSrc

      public String getSrc()
      Returns the URI of the source that contains the script codes.

      Default: null.

    • setSrc

      public void setSrc(String src)
      Sets the URI of the source that contains the script codes.

      You either add the script codes directly with the Label children, or set the URI to load the script codes with setSrc(java.lang.String). But, not both.

      Parameters:
      src - the URI of the source that contains the script codes
    • isDefer

      public boolean isDefer()
      Returns whether to defer the execution of the script codes until the widget is instantiated and mounted.

      Default: false.

      Specifying false (default), if you want to do the desktop-level (or class-level) initialization, such as defining a widget class or a global function.

      Specifying true, if you want to access widgets. Notice that this references to this script widget.

    • setDefer

      public void setDefer(boolean defer)
      Sets whether to defer the execution of the script codes.
      See Also:
    • getContent

      public String getContent()
      Returns the content of the script element. By content we mean the JavaScript codes that will be enclosed by the HTML SCRIPT element.

      Default: null.

      Deriving class can override this method to return whatever it prefers (ignored if null).

      Since:
      3.0.0
    • setContent

      public void setContent(String content)
      Sets the content of the script element. By content we mean the JavaScript codes that will be enclosed by the HTML SCRIPT element.
      Since:
      3.0.0
    • getPackages

      public String getPackages()
      Returns the list of packages to load before evaluating the script defined in getContent(). It is meaning only if getContent() not null.

      Default: null.

      Since:
      5.0.0
    • setPackages

      public void setPackages(String packages)
      Sets the list of packages to load before evaluating the script defined in getContent(). If more than a package to load, separate them with comma.
      Since:
      5.0.0
    • renderProperties

      protected void renderProperties(ContentRenderer renderer) throws IOException
      Description copied from class: AbstractComponent
      Called by (ComponentCtrl.redraw(java.io.Writer)) to render the properties, excluding the enclosing tag and children.

      Default: it renders AbstractComponent.getId() if it was assigned, and event names if listened (and listed in AbstractComponent.getClientEvents()).

      Note: it doesn't render AbstractComponent.getWidgetClass(), AbstractComponent.getUuid() and AbstractComponent.getMold(), which are caller's job.

      Overrides:
      renderProperties in class AbstractComponent
      Throws:
      IOException
    • isChildable

      protected boolean isChildable()
      Not childable.
      Overrides:
      isChildable in class AbstractComponent