04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 13: Scripting Forms<br />

Clipboard data is accessible via the clipboardData object that exists either on the window object<br />

(in IE) or on the event object (in Safari and Chrome); Firefox does not support the clipboardData<br />

object. In Safari and Chrome, the clipboardData object is available only during clipboard events to<br />

prevent unauthorized clipboard access; IE exposes the clipboardData object all the time. For cross -<br />

browser compatibility, it ’ s best to use this object only during clipboard events.<br />

There are three methods on the clipboardData object: getData() , setData() , and clearData() . The<br />

getData() method retrieves string data from the clipboard and accepts a single argument, which is<br />

the format for the data to retrieve. IE specifies two options: “ text ” and “ URL “ . Safari and Chrome<br />

expect a MIME type, but will accept “ text ” as equivalent to “ text/plain ” .<br />

The setData() method is similar: its first argument is the data type, and its second argument is the text<br />

to place on the clipboard. Once again, IE supports “ text ” and “ URL ” whereas Safari and Chrome expect<br />

a MIME type. Unlike getData() , however, Safari and Chrome won ’ t recognize the “ text ” type. Both<br />

browsers return true if the text was placed onto the clipboard successfully, or false if not. To even out<br />

the differences, the following cross - browser methods can be added to EventUtil :<br />

var EventUtil = {<br />

};<br />

//more code here<br />

getClipboardText: function(event){<br />

var clipboardData = (event.clipboardData || window.clipboardData);<br />

return clipboardData.getData(“text”);<br />

},<br />

//more code here<br />

setClipboardText: function(event, value){<br />

if (event.clipboardData){<br />

return event.clipboardData.setData(“text/plain”, value);<br />

} else if (window.clipboardData){<br />

return window.clipboardData.setData(“text”, value);<br />

}<br />

},<br />

//more code here<br />

The getClipboardText() method is relatively simple. It needs only to identify the location of the<br />

clipboardData object and then call getData() with a type of “ text ” . Its companion method,<br />

setClipboardText() , is slightly more involved. Once the clipboardData object is located,<br />

setData() is called with the appropriate type for each implementation ( “ text/plain ” for Safari and<br />

Chrome; “ text ” for IE).<br />

Reading text from the clipboard is helpful when you have a text box that expects only certain characters<br />

or a certain format of text. For example, if a text box allows only numbers, then pasted values must also<br />

be inspected to ensure that the value is valid. In the paste event, you can determine if the text on the<br />

clipboard is invalid, and if so, cancel the default behavior as shown in the following example:<br />

448

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

Saved successfully!

Ooh no, something went wrong!