04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13: Scripting Forms<br />

controls, there will be only one option selected, whereas multiselect controls may have zero or more<br />

options selected. The same code can be used for both select types, because the restriction on the number<br />

of selections is enforced by the browser. When an option is selected, you need to determine which value<br />

to use. If the value attribute is not present, the text should be used instead, although a value attribute<br />

with an empty string is completely valid. To check this, you ’ ll need to use hasAttribute() in DOM -<br />

compliant browsers and the attribute ’ s specified property in IE.<br />

If a < fieldset > element is in the form, it appears in the elements collection but has no type property.<br />

So if type is undefined, no serialization is necessary. The same is true for all types of buttons and file<br />

input fields (File input fields contain the content of the file in form submissions; however, the fields can ’ t<br />

be mimicked, so they are typically omitted in serialization). For radio and check box controls, the<br />

checked property is inspected and if it is set to false , the switch statement is exited. If checked is<br />

true , then the code continues executing in the default statement, which encodes the name and value of<br />

the field and adds it to the parts array. The last part of the function uses join() to format the string<br />

correctly with ampersands between fields.<br />

The serialize() function outputs the string in query string format, though it can easily be adapted to<br />

serialize the form into another format.<br />

Rich Text Editing<br />

One of the most requested features for web applications was the ability to edit rich text on a web page<br />

(also called what you see is what you get, or WYSIWYG , editing). Though no specification covers this, a de<br />

facto standard has emerged from functionality originally introduced by IE and now supported by Opera,<br />

Safari, Chrome, and Firefox. The basic technique is to embed an iframe containing a blank HTML file in<br />

the page. Through the designMode property, this blank document can be made editable, at which point<br />

you ’ re editing the HTML of the page ’ s < body > element. The designMode property has two possible<br />

values: “ off ” (the default) and “ on ” . When set to “ on ” , an entire document becomes editable (showing<br />

a caret), allowing you to edit text as if you were using a word processor complete with keystrokes for<br />

making text bold, italic, and so forth.<br />

A very simple blank HTML page is used as the source of the iframe . Here’s an example:<br />

< html ><br />

< head ><br />

< title > Blank Page for Rich Text Editing < /title ><br />

< /head ><br />

< body ><br />

< /body ><br />

< /html ><br />

This page is loaded inside an iframe as any other page would be. To allow it to be edited, you must set<br />

designMode to “ on ” , but this can happen only after the document is fully loaded. In the containing<br />

page, you ’ ll need to use the onload event handler to indicate the appropriate time to set designMode , as<br />

shown in the following example:<br />

< iframe name=”richedit” style=”height: 100px; width: 100px” src=”blank.htm” > < /iframe ><br />

< script type=”text/<strong>javascript</strong>” ><br />

EventUtil.addHandler(window, “load”, function(){<br />

458

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!