blob: b5b4a2a7c84a4e5bcdb5a525bb9f6ffc2fba37cd [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- $Id$ -->
<html>
<head>
<title>Script</title>
<link rel="stylesheet" type="text/css" href="Tapestry.css" title="style">
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left"><A href="Rollover.html"><IMG alt=Rollover src="common-images/prev.png"></a></td>
<td align="middle"><A href="index.html"><IMG alt="Component Index" src="common-images/home.png" ></a></td>
<td align="right"><A href="Select.html"><IMG alt=Select src="common-images/next.png"></a></td>
<tr>
<tr>
<td colspan="3"><hr></td>
</tr>
<tr>
<td colspan="3">
<table border="0" cellpadding="4" cellspacing="4" width="100%">
<tr valign="top">
<td>
<table>
<tr>
<td><font size="+2"><b>Script</b></font></td>
</tr>
<tr>
<td>
<A href="../api/org/apache/tapestry/html/Script.html">
org.apache.tapestry.html.Script</a>
</td>
</tr>
</table>
</td>
<td>
<table align="right" valign="middle" bgcolor="#c0c0c0" cellpadding="8">
<tr>
<td>Non Visual Component</td>
</tr>
</table>
</td>
</tr>
<tr valign="center">
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td colspan="2">
<b>Description</b>
<br>
A component that accesses a script file and adds JavaScript functions and
statements to the response page. The specified script file is read and
parsed and substitutions are made before the final scripting code is inserted
into the page. This allows the JavaScript to be tailored to the ids and names
that are generated by Tapestry.
<p>
Components within a Script's body may access the input and <emphasis>output</emphasis>
parameters of the Script via the OGNL expression
<code>components.<i>scriptId</i>.symbols.<i>name</i></code>.
<p>
Note a <A href="Body.html">Body</a> component is required when using the Script
element. The Body component is used to write out the JavaScript after the
HTML &lt;body&gt; section and attach any initialization code to the body's
"onload" event handler.
<p>
See the Developers Guide
<A href="../DevelopersGuide/script.html">Tapestry and JavaScript</a> for a complete description of Tapestry's JavaScript
integration.</p>
</td>
</tr>
<tr>
<td colspan="2">
<b>See Also</b>
<br>
<A href="Body.html">Body</a>
</td>
</tr>
<tr>
<td colspan="2">
<b>Parameters</b>
<br>
<table border="1" cellpadding="4" cellspacing="4" class="parameters">
<tr>
<th>Name</th>
<th>Type</th>
<th>Direction</th>
<th>Required</th>
<th>Default</th>
<th>Description</th>
</tr>
<tr>
<td>script</td>
<td>String</td>
<td>in</td>
<td>yes</td>
<td>&nbsp;</td>
<td>The path of a resource (on the classpath) containing the script.</td>
</tr>
<tr>
<td>symbols</td>
<td>
<a href="http://java.sun.com/products/jdk/1.2/docs/api/java/util/Map.html">
<tt>Map</tt></a>
</td>
<td>in</td>
<td>no</td>
<td>&nbsp;</td>
<td>
The base set of symbols to be provided to the
<A href="../api/org/apache/tapestry/IScript.html">IScript</a>. To this is added
(in a copy of the Map) any informal parameters.
</td>
</tr>
</table>
<P>Body: <STRONG>allowed</STRONG><BR>
Informal parameters:
<STRONG>allowed</STRONG>
<br>
Reserved
parameters: <EM>none</EM> </P>
</td>
</tr>
<tr>
<td colspan="2">
<b>Examples</b>
<p>
See the <A href="PropertySelection.html#Example2">PropertySelection</a>
example use of SelectSubmit script to submit a <A href="Form.html">form</a>
when a user selects a drop down list item.
<p>
In this example a Script is used set the focus to the first text field of the
login form. In the script file <b>&lt;![CDATA[</b> .. <b>]]&gt;</b> tags are
used to wrap the JavaScript code to prevent '<tt>&lt;</tt>' and
'<tt>&amp;&amp;</tt>' character XML parsing errors.
<p>
Note Tapestry will not perform property substitutions in CDATA blocks when
using the <tt>&lt;expression&gt;</tt> style syntax, instead use the
<tt>${expression}</tt> syntax.
<p>
<table class="tapestry-examples" cellpadding="4">
<form>
<tr>
<td>Username:</td><td><input size="12"
></td>
</tr>
<tr>
<td>Password:</td><td><input type="password" size="12"></td>
</tr>
<tr align="right">
<td>&nbsp;</td><td><input type="submit" value="Login"></td>
</tr>
</form></table>
<pre>
&lt;body jwcid="@<a href="Body.html">Body</a>"&gt;
&lt;span jwcid="<span class="jwcid">@<a href="Script.html">Script</a></span>" script=&quot;/com/mycorp/scripts/FormFocus.script&quot;/&gt;
&lt;form jwcid="@<a href="Form.html">Form</a>" listener="ognl:listener.formSubmit"&gt;
&lt;table cellpadding=
"4"&gt;
&lt;tr&gt;&lt;td&gt;Username:&lt;/td&gt;&lt;td&gt;&lt;input jwcid="@<a href="TextField.html">TextField</a>" value="ognl:visit.username" size="12"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Password:&lt;/td&gt;&lt;td&gt;&lt;input jwcid="@<a href="TextField.html">TextField</a>" value="ognl:visit.password" hidden=&quot;ognl:true&quot; size="12"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr align="right"&gt;
&lt;td colspan="2"&gt;&lt;input type="submit" value="Login"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;!-- Sets the focus to the first form element which is not hidden and not disabled. --&gt;
&lt;!-- /com/mycorp/scripts/FormFocus.script --&gt;
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE script PUBLIC<br>
&quot;-//Apache Software Foundation//Tapestry Script Specification 3.0//EN&quot;<br>
&quot;http://jakarta.apache.org/tapestry/dtd/Script_3_0.dtd&quot;&gt;<br>
&lt;script&gt;
&lt;body&gt;
<b>&lt;![CDATA[</b>
function setFocus() {
if (document.forms[0]) {
for (i = 0; i &lt; document.forms[0].elements.length; i++) {
if (document.forms[0].elements[i].type != "hidden" &amp;&amp;
document.forms[0].elements[i].disabled != true) {
document.forms[0].elements[i].focus();
return;
}
}
}
}
<b>]]&gt;</b>
&lt;/body&gt;
&lt;initialization&gt;
setFocus();
&lt;/initialization&gt;
&lt;/script&gt;
</pre>
</td>
</tr>
<tr>
<td colspan="2"><hr></td>
</tr>
<tr>
<td colspan="2">
<p>
This alternative <tt>InputFocus.script</tt> specifies the actual input field
to give the focus to. The target input field is identified by an informal
parameter named <b>component</b>. The script then uses
<tt>${expression}</tt> element name insertions to complete the JavaScript.
<p>
Note when using this script, the target input field component must be declared
before the script component in the HTML template, and within the
<A href="Form.html">Form</a> block, so that the target field component can be
resolved by the <A href="Script.html">Script</a> component.
<p>
<pre>
&lt;body jwcid="@<a href="Body.html">Body</a>"&gt;
&lt;form jwcid="@<a href="Form.html">Form</a>" listener="ognl:listener.formSubmit"&gt;
&lt;table cellpadding=
"4"&gt;
&lt;tr&gt;&lt;td&gt;Username:&lt;/td&gt;&lt;td&gt;&lt;input jwcid="usernameTextField@<a href="TextField.html">TextField</a>" value="ognl:visit.username" size="12"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Password:&lt;/td&gt;&lt;td&gt;&lt;input jwcid="@<a href="TextField.html">TextField</a>" value="ognl:visit.password" hidden=&quot;ognl:true&quot; size="12"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr align="right"&gt;
&lt;td colspan="2"&gt;&lt;input type="submit" value="Login"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;span jwcid="<span class="jwcid">@<a href="Script.html">Script</a></span>" script=&quot;/com/mycorp/scripts/FormFocus.script&quot; <b>component</b>="ognl:components.usernameTextField"/&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;!-- Sets the focus to the specified input field if not hidden and not disabled. --&gt;
&lt;!-- /com/mycorp/scripts/InputFocus.script --&gt;
&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE script PUBLIC<br>
&quot;-//Apache Software Foundation//Tapestry Script Specification 3.0//EN&quot;<br>
&quot;http://jakarta.apache.org/tapestry/dtd/Script_3_0.dtd&quot;&gt;<br>
&lt;!--
Selects the specified form input field on body load if the input type is not
"hidden" and the input field is not disabled.
Input symbols:
component: the component input field to give focus
--&gt;
&lt;input-symbol key=&quot;<b>component</b>&quot; class=&quot;org.apache.tapestry.form.AbstractFormComponent&quot; required=&quot;yes&quot;/&gt;<br>
&lt;let key=&quot;formObject&quot;&gt;<br> document.${<b>component</b>.form.name}<br>&lt;/let&gt;</pre>
<pre>
&lt;let key=&quot;componentObject&quot;&gt;<br> ${formObject}.${<b>component</b>.name}<br>&lt;/let&gt;</pre>
<pre></pre>
<pre>
&lt;script&gt;
&lt;body&gt;
function setFocus() {
var inputField = ${componentObject.name};
if (inputField.type != "hidden") {
if (inputField.disabled != true) {
inputField.focus();
}
} else {
window.alert('InputFocus.script cannot set focus on a hidden field');
}
}
&lt;/body&gt;
&lt;initialization&gt;
setFocus();
&lt;/initialization&gt;
&lt;/script&gt;
</pre>
</td>
</tr>
</table></TD></TR>
<tr>
<td colspan="3"><hr></td>
</tr>
<tr>
<td align="left"><A href="Rollover.html"><IMG alt=Rollover src="common-images/prev.png"></a></td>
<td align="middle"><A href="index.html"><IMG alt="Component Index" src="common-images/home.png" ></a></td>
<td align="right"><A href="Select.html"><IMG alt=Select src="common-images/next.png"></a></td>
</tr></TABLE>
</body>
</html>