Short URL: http://html5.org/r/3791
| SVN | Bug | Comment | Time (UTC) |
|---|---|---|---|
| 3791 | <script> example. | 2009-09-10 09:57 |
Index: source
===================================================================
--- source (revision 3790)
+++ source (revision 3791)
@@ -13155,9 +13155,50 @@
title="dom-outerHTML">outerHTML</code> attributes, they do not
execute at all.</p>
+ <div class="example">
+ <p>The following sample shows how a script element can be used to
+ define a function that is then used by other parts of the
+ document. It also shows how a <code>script</code> element can be
+ used to invoke script while the document is being parsed, in this
+ case to initialize the form's output.</p>
+ <pre><script>
+ function calculate(form) {
+ var price = 52000;
+ if (form.elements.brakes.checked)
+ price += 1000;
+ if (form.elements.radio.checked)
+ price += 2500;
+ if (form.elements.turbo.checked)
+ price += 5000;
+ if (form.elements.sticker.checked)
+ price += 250;
+ form.elements.result.value = price;
+ }
+</script>
+<form name="pricecalc" onsubmit="return false">
+ <fieldset>
+ <legend>Work out the price of your car</legend>
+ <p>Base cost: £52000.</p>
+ <p>Select additional options:</p>
+ <ul>
+ <li><label><input type=checkbox name=brakes> Ceramic brakes (£1000)</label></li>
+ <li><label><input type=checkbox name=radio> Satellite radio (£2500)</label></li>
+ <li><label><input type=checkbox name=turbo> Turbo charger (£5000)</label></li>
+ <li><label><input type=checkbox name=sticker> "XZ" sticker (£250)</label></li>
+ </ul>
+ <p>Total: £<output name=result onformchange="calculate(form)"></output></p>
+ </fieldset>
+ <script>
+ document.forms.pricecalc.dispatchFormChange();
+ </script>
+</form></pre>
+ </div>
+
+
+
<h5 id="scriptingLanguages">Scripting languages</h5>
<div class="impl">