Script"

From Documentation
 
(11 intermediate revisions by 5 users not shown)
Line 9: Line 9:
 
= Employment/Purpose =
 
= Employment/Purpose =
 
The script component is used to specify the script codes running at the browser. Notice that, unlike zscript, the script codes are running at the browser. They are usually written in JavaScript which is supported by the most of browsers. The simplest format is as follows.
 
The script component is used to specify the script codes running at the browser. Notice that, unlike zscript, the script codes are running at the browser. They are usually written in JavaScript which is supported by the most of browsers. The simplest format is as follows.
 
==Alternatives==
 
 
Instead of using the script component, you could use [[ZUML_Reference/ZUML/Processing_Instructions/script|the script directive]] instead. It does not support defer, but it is more efficient since no component is created.
 
 
<source lang="xml">
 
<?script src="~./js/zk.debug.wpd"?>
 
<?script content="jq.IE6_ALPHAFIX='.png';"?>
 
</source>
 
 
where the first statement loads the debug utility and the second generates JavaScript code snippet directly.
 
 
Another alternative is HTML SCRIPT.  For example, we could define global variables and functions as follows
 
 
<source lang="xml">
 
<script xmlns:n="native"><!-- use the native namespace -->
 
var a_global_variable;
 
function a_global_function () {
 
alert("native script");
 
}
 
alert("you can not access this as widget but evaluated immediately");
 
</script>
 
</source>
 
  
 
= Example =
 
= Example =
Line 37: Line 14:
 
[[Image:ZKComRef_Script_Example.png ]]  
 
[[Image:ZKComRef_Script_Example.png ]]  
  
<syntax lang="xml" >
+
<syntaxhighlight lang="xml" >
<zk>
+
<window id="win">
<window id="win">
+
<button label="change color" onClick='Clients.evalJavaScript("myfunc()")' />
<button label="change color" onClick='Clients.evalJavaScript("myfunc()")' />
+
</window>
</window>
+
<script type="text/javascript">
<script type="text/javascript">
+
function myfunc() {
function myfunc() {
+
    jq("$win").css("backgroundColor", "blue");
    jq("$win").css("backgroundColor", "blue");
+
}
}
 
 
  </script>
 
  </script>
</zk>
+
</syntaxhighlight>
</syntax>
 
  
 
=Defer the Evaluation=
 
=Defer the Evaluation=
  
By default, the specified JavaScript code will be evaluated as soon as the page is loaded. There is an attribute called defer. By specifying true, the JavaScript code won't be evaluated until all widgets are created and bound to the DOM tree.
+
By default, the specified JavaScript code will be evaluated as soon as the page is loaded. By specifying <code>defer="true"</code>, the JavaScript code won't be evaluated until all ZK widgets are created and bound to the DOM tree.
  
<syntax lang="xml">
+
<syntaxhighlight lang="xml">
 
<textbox id="inp"/>
 
<textbox id="inp"/>
 
<script defer="true">
 
<script defer="true">
 
   this.$f("inp").setValue("initialized");
 
   this.$f("inp").setValue("initialized");
 
</script>
 
</script>
</syntax>
+
</syntaxhighlight>
  
 
The defer attribute can be used with a JavaScript file as shown below. Then, the JavaScript file will be loaded after all widgets are created and bound to the DOM tree.
 
The defer attribute can be used with a JavaScript file as shown below. Then, the JavaScript file will be loaded after all widgets are created and bound to the DOM tree.
  
<syntax lang="xml">
+
<syntaxhighlight lang="xml">
 
<script src="/js/foo.js" defer="true"/>
 
<script src="/js/foo.js" defer="true"/>
</syntax>
+
</syntaxhighlight>
 +
 
 +
This is required if the script to load intends to manipulate a ZK widget's DOM elements. <code>defer="true"</code> can ensure that all widgets' DOM elements are already generated.
 +
 
 +
=Alternatives=
 +
 
 +
== script directive==
 +
Instead of using the script component, you could use [[ZUML_Reference/ZUML/Processing_Instructions/script|the script directive]] instead. It does not support <code>defer</code>, but it is more efficient since no component is created.
 +
 
 +
<source lang="xml">
 +
<?script src="~./js/zk.debug.wpd"?>
 +
<?script content="jq.IE6_ALPHAFIX='.png';"?>
 +
</source>
 +
 
 +
where the first statement loads the debug utility and the second generates a JavaScript code snippet directly.
 +
 
 +
==HTML script tag ==
 +
Another alternative is the HTML SCRIPT tag.  For example, we could define global variables and functions as follows
 +
 
 +
<source lang="xml">
 +
<n:script xmlns:n="native"><!-- use the native namespace -->
 +
var a_global_variable;
 +
function a_global_function () {
 +
alert("native script");
 +
}
 +
alert("you can not access this as widget but evaluated immediately");
 +
</n:script>
 +
</source>
  
 
=Supported Events=
 
=Supported Events=
  
{| border="1" | width="100%"
+
{| class='wikitable' | width="100%"
 
! <center>Name</center>
 
! <center>Name</center>
 
! <center>Event Type</center>
 
! <center>Event Type</center>
Line 84: Line 86:
 
=Use Cases=
 
=Use Cases=
  
{| border='1px' | width="100%"
+
{| class='wikitable' | width="100%"
 
! Version !! Description !! Example Location
 
! Version !! Description !! Example Location
 
|-
 
|-
Line 90: Line 92:
 
| Overview and Tutorial
 
| Overview and Tutorial
 
|[[Small_Talks/2010/April/Client_Side_Programming |Client Side Programming]]
 
|[[Small_Talks/2010/April/Client_Side_Programming |Client Side Programming]]
http://docs.zkoss.org/wiki/ZK5:_Client_Computing_with_ZUML
+
[[ZK_Client-side_Reference/General_Control|ZK Client-side Reference: General Control]]
 
|}
 
|}
  
=Version History=
+
 
{{LastUpdated}}
 
{| border='1px' | width="100%"
 
! Version !! Date !! Content
 
|-
 
| &nbsp;
 
| &nbsp;
 
| &nbsp;
 
|}
 
  
 
{{ZKComponentReferencePageFooter}}
 
{{ZKComponentReferencePageFooter}}

Latest revision as of 06:47, 21 December 2023

Script

Employment/Purpose

The script component is used to specify the script codes running at the browser. Notice that, unlike zscript, the script codes are running at the browser. They are usually written in JavaScript which is supported by the most of browsers. The simplest format is as follows.

Example

ZKComRef Script Example.png

<window id="win">
	<button label="change color" onClick='Clients.evalJavaScript("myfunc()")' />
</window>
<script type="text/javascript">
	function myfunc() {
	    jq("$win").css("backgroundColor", "blue");
	}
 </script>

Defer the Evaluation

By default, the specified JavaScript code will be evaluated as soon as the page is loaded. By specifying defer="true", the JavaScript code won't be evaluated until all ZK widgets are created and bound to the DOM tree.

<textbox id="inp"/>
<script defer="true">
   this.$f("inp").setValue("initialized");
</script>

The defer attribute can be used with a JavaScript file as shown below. Then, the JavaScript file will be loaded after all widgets are created and bound to the DOM tree.

<script src="/js/foo.js" defer="true"/>

This is required if the script to load intends to manipulate a ZK widget's DOM elements. defer="true" can ensure that all widgets' DOM elements are already generated.

Alternatives

script directive

Instead of using the script component, you could use the script directive instead. It does not support defer, but it is more efficient since no component is created.

<?script src="~./js/zk.debug.wpd"?>
<?script content="jq.IE6_ALPHAFIX='.png';"?>

where the first statement loads the debug utility and the second generates a JavaScript code snippet directly.

HTML script tag

Another alternative is the HTML SCRIPT tag. For example, we could define global variables and functions as follows

<n:script xmlns:n="native"><!-- use the native namespace -->
	var a_global_variable;
	function a_global_function () {
		alert("native script");
	}
	alert("you can not access this as widget but evaluated immediately");
</n:script>

Supported Events

Name
Event Type
None None

Supported Children

*NONE

Use Cases

Version Description Example Location
5.0 Overview and Tutorial Client Side Programming

ZK Client-side Reference: General Control




Last Update : 2023/12/21

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.