1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect3 id="zend.view.helpers.initial.headscript">
4 <title>HeadScript Helper</title>
7 The <acronym>HTML</acronym> <emphasis><script></emphasis> element is used to either
8 provide inline client-side scripting elements or link to a remote resource
9 containing client-side scripting code. The <classname>HeadScript</classname>
10 helper allows you to manage both.
14 The <classname>HeadScript</classname> helper supports the following methods for
15 setting and adding scripts:
21 <command>appendFile($src, $type = 'text/javascript', $attrs = array())</command>
27 <command>offsetSetFile($index, $src, $type = 'text/javascript', $attrs =
34 <command>prependFile($src, $type = 'text/javascript', $attrs = array())</command>
40 <command>setFile($src, $type = 'text/javascript', $attrs = array())</command>
46 <command>appendScript($script, $type = 'text/javascript', $attrs =
53 <command>offsetSetScript($index, $script, $type = 'text/javascript', $attrs =
60 <command>prependScript($script, $type = 'text/javascript', $attrs =
67 <command>setScript($script, $type = 'text/javascript', $attrs = array())</command>
73 In the case of the *<methodname>File()</methodname> methods, <varname>$src</varname> is
74 the remote location of the script to load; this is usually in the form
75 of a <acronym>URL</acronym> or a path. For the *<methodname>Script()</methodname> methods,
76 <varname>$script</varname> is the client-side scripting directives you wish to
81 <title>Setting Conditional Comments</title>
84 <classname>HeadScript</classname> allows you to wrap the script tag in conditional
85 comments, which allows you to hide it from specific browsers. To add the conditional
86 tags, pass the conditional value as part of the <varname>$attrs</varname> parameter in
90 <example id="zend.view.helpers.initial.headscript.conditional">
91 <title>Headscript With Conditional Comments</title>
93 <programlisting language="php"><![CDATA[
95 $this->headScript()->appendFile(
98 array('conditional' => 'lt IE 7')
105 <classname>HeadScript</classname> also allows capturing scripts; this can be
106 useful if you want to create the client-side script programmatically,
107 and then place it elsewhere. The usage for this will be showed in an
112 Finally, you can also use the <methodname>headScript()</methodname> method to
113 quickly add script elements; the signature for this is
114 <methodname>headScript($mode = 'FILE', $spec, $placement = 'APPEND')</methodname>.
115 The <varname>$mode</varname> is either 'FILE' or 'SCRIPT', depending on if
116 you're linking a script or defining one. <varname>$spec</varname> is either
117 the script file to link or the script source itself.
118 <varname>$placement</varname> should be either 'APPEND', 'PREPEND', or 'SET'.
122 <classname>HeadScript</classname> overrides each of <methodname>append()</methodname>,
123 <methodname>offsetSet()</methodname>, <methodname>prepend()</methodname>, and
124 <methodname>set()</methodname> to enforce usage of the special methods as listed above.
125 Internally, it stores each item as a <property>stdClass</property> token, which it later
126 serializes using the <methodname>itemToString()</methodname> method. This allows you
127 to perform checks on the items in the stack, and optionally modify these
128 items by simply modifying the object returned.
132 The <classname>HeadScript</classname> helper is a concrete implementation of the
133 <link linkend="zend.view.helpers.initial.placeholder">Placeholder helper</link>.
137 <title>Use InlineScript for HTML Body Scripts</title>
140 <classname>HeadScript</classname>'s sibling helper, <link
141 linkend="zend.view.helpers.initial.inlinescript">InlineScript</link>,
142 should be used when you wish to include scripts inline in the <acronym>HTML</acronym>
143 <emphasis>body</emphasis>. Placing scripts at the end of your document is a
144 good practice for speeding up delivery of your page, particularly
145 when using 3rd party analytics scripts.
150 <title>Arbitrary Attributes are Disabled by Default</title>
153 By default, <classname>HeadScript</classname> only will render
154 <emphasis><script></emphasis> attributes that are blessed by the W3C.
155 These include 'type', 'charset', 'defer', 'language', and 'src'.
156 However, some javascript frameworks, notably <ulink
157 url="http://www.dojotoolkit.org/">Dojo</ulink>, utilize custom
158 attributes in order to modify behavior. To allow such attributes,
159 you can enable them via the
160 <methodname>setAllowArbitraryAttributes()</methodname> method:
163 <programlisting language="php"><![CDATA[
164 $this->headScript()->setAllowArbitraryAttributes(true);
168 <example id="zend.view.helpers.initial.headscript.basicusage">
169 <title>HeadScript Helper Basic Usage</title>
172 You may specify a new script tag at any time. As noted above, these
173 may be links to outside resource files or scripts themselves.
176 <programlisting language="php"><![CDATA[
178 $this->headScript()->appendFile('/js/prototype.js')
179 ->appendScript($onloadScript);
183 Order is often important with client-side scripting; you may need to
184 ensure that libraries are loaded in a specific order due to
185 dependencies each have; use the various append, prepend, and
186 offsetSet directives to aid in this task:
189 <programlisting language="php"><![CDATA[
190 // Putting scripts in order
192 // place at a particular offset to ensure loaded last
193 $this->headScript()->offsetSetFile(100, '/js/myfuncs.js');
195 // use scriptaculous effects (append uses next index, 101)
196 $this->headScript()->appendFile('/js/scriptaculous.js');
198 // but always have base prototype script load first:
199 $this->headScript()->prependFile('/js/prototype.js');
203 When you're finally ready to output all scripts in your layout
204 script, simply echo the helper:
207 <programlisting language="php"><![CDATA[
208 <?php echo $this->headScript() ?>
212 <example id="zend.view.helpers.initial.headscript.capture">
213 <title>Capturing Scripts Using the HeadScript Helper</title>
216 Sometimes you need to generate client-side scripts programmatically.
217 While you could use string concatenation, heredocs, and the like,
218 often it's easier just to do so by creating the script and
219 sprinkling in <acronym>PHP</acronym> tags. <classname>HeadScript</classname> lets you do
220 just that, capturing it to the stack:
223 <programlisting language="php"><![CDATA[
224 <?php $this->headScript()->captureStart() ?>
225 var action = '<?php echo $this->baseUrl ?>';
226 $('foo_form').action = action;
227 <?php $this->headScript()->captureEnd() ?>
231 The following assumptions are made:
237 The script will be appended to the stack. If you wish for it
238 to replace the stack or be added to the top, you will need
239 to pass 'SET' or 'PREPEND', respectively, as the first
240 argument to <methodname>captureStart()</methodname>.
246 The script <acronym>MIME</acronym> type is assumed to be 'text/javascript'; if
247 you wish to specify a different type, you will need to pass it
248 as the second argument to <methodname>captureStart()</methodname>.
254 If you wish to specify any additional attributes for the
255 <emphasis><script></emphasis> tag, pass them in an array as
256 the third argument to <methodname>captureStart()</methodname>.