public class Script
extends org.zkoss.zk.ui.AbstractComponent
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/"?/>
Constructor and Description |
---|
Script() |
Script(String content)
Constructs a script component with the specified content.
|
Modifier and Type | Method and Description |
---|---|
String |
getCharset()
Returns the character enconding of the source.
|
String |
getContent()
Returns the content of the script element.
|
String |
getPackages()
Returns the list of packages to load before evaluating the script
defined in
getContent() . |
String |
getSrc()
Returns the URI of the source that contains the script codes.
|
String |
getType()
Deprecated.
As of release 5.0.0, it is meaningless since
text/javascript is always assumed.
|
protected boolean |
isChildable()
Not childable.
|
boolean |
isDefer()
Returns whether to defer the execution of the script codes
until the widget is instantiated and mounted.
|
protected void |
renderProperties(org.zkoss.zk.ui.sys.ContentRenderer renderer) |
void |
setCharset(String charset)
Sets the character encoding of the source.
|
void |
setContent(String content)
Sets the content of the script element.
|
void |
setDefer(boolean defer)
Sets whether to defer the execution of the script codes.
|
void |
setPackages(String packages)
Sets the list of packages to load before evaluating the script
defined in
getContent() . |
void |
setSrc(String src)
Sets the URI of the source that contains the script codes.
|
void |
setType(String type)
Deprecated.
As of release 5.0.0, it is meaningless since
text/javascript is always assumed.
|
addAnnotation, addAnnotation, addCallback, addClientEvent, addEventHandler, addEventListener, addEventListener, addForward, addForward, addForward, addForward, addMoved, addRedrawCallback, addScopeListener, addShadowRoot, addShadowRootBefore, addSharedEventHandlerMap, appendChild, applyProperties, beforeChildAdded, beforeChildRemoved, beforeParentChanged, clone, detach, didActivate, didActivate, didDeserialize, didDeserialize, disableBindingAnnotation, disableClientUpdate, enableBindingAnnotation, getAnnotatedProperties, getAnnotatedPropertiesBy, getAnnotation, getAnnotation, getAnnotations, getAnnotations, getAnnotations, getAttribute, getAttribute, getAttribute, getAttributeOrFellow, getAttributes, getAttributes, getAuService, getAutag, getCallback, getChildren, getClientAttribute, getClientDataAttribute, getClientEvents, getDefaultMold, getDefinition, getDesktop, getEventHandler, getEventHandlerNames, getEventListenerMap, getEventListeners, getExtraCtrl, getFellow, getFellow, getFellowIfAny, getFellowIfAny, getFellows, getFirstChild, getId, getLastChild, getListenerIterator, getMold, getNextSibling, getPage, getParent, getPreviousSibling, getPropertyAccess, getRedrawCallback, getRoot, getShadowFellowIfAny, getShadowRoots, getShadowVariable, getShadowVariable, getShadowVariable0, getSpaceOwner, getSpecialRendererOutput, getStubonly, getSubBindingAnnotationCount, getTemplate, getTemplateNames, getUuid, getWidgetAttribute, getWidgetAttributeNames, getWidgetClass, getWidgetListener, getWidgetListenerNames, getWidgetOverride, getWidgetOverrideNames, hasAttribute, hasAttribute, hasAttribute, hasAttributeOrFellow, hasBindingAnnotation, hasFellow, hasFellow, hasSubBindingAnnotation, insertBefore, invalidate, invalidatePartial, isInitialized, isInvalidated, isListenerAvailable, isVisible, onChildAdded, onChildRemoved, onPageAttached, onPageDetached, onWrongValue, query, queryAll, redraw, redrawChildren, removeAttribute, removeAttribute, removeAttribute, removeCallback, removeChild, removeEventListener, removeForward, removeForward, removeRedrawCallback, removeScopeListener, removeShadowRoot, render, render, render, replace, response, response, response, service, service, sessionDidActivate, sessionWillPassivate, setAttribute, setAttribute, setAttribute, setAuService, setAutag, setClientAttribute, setClientDataAttribute, setDefinition, setDefinition, setId, setMold, setPage, setPageBefore, setParent, setStubonly, setStubonly, setSubBindingAnnotationCount, setTemplate, setVisible, setVisibleDirectly, setWidgetAttribute, setWidgetClass, setWidgetListener, setWidgetOverride, smartUpdate, smartUpdate, smartUpdate, smartUpdate, smartUpdate, smartUpdate, smartUpdate, smartUpdate, smartUpdate, smartUpdateWidgetListener, smartUpdateWidgetOverride, toString, updateByClient, updateSubBindingAnnotationCount, willPassivate, willPassivate, willSerialize, willSerialize
public Script()
public Script(String content)
content
- the content (the code snippet).public String getType()
public void setType(String type)
public String getCharset()
getSrc()
.
Default: null.
public void setCharset(String charset)
setSrc(java.lang.String)
.
Refer to HTML Character Setsfor more information.
public String getSrc()
Default: null.
public void setSrc(String src)
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.
src
- the URI of the source that contains the script codespublic boolean isDefer()
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.
public void setDefer(boolean defer)
isDefer()
public String getContent()
Default: null.
Deriving class can override this method to return whatever it prefers (ignored if null).
public void setContent(String content)
public String getPackages()
getContent()
.
It is meaning only if getContent()
not null.
Default: null.
public void setPackages(String packages)
getContent()
.
If more than a package to load, separate them with comma.protected void renderProperties(org.zkoss.zk.ui.sys.ContentRenderer renderer) throws IOException
renderProperties
in class org.zkoss.zk.ui.AbstractComponent
IOException
protected boolean isChildable()
isChildable
in class org.zkoss.zk.ui.AbstractComponent
Copyright © 2022. All rights reserved.