15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

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

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

Chapter 1 ✦ Generic HTML Element Objects (Chapter 15)<br />

Example<br />

See Listing 15-30 earlier in this chapter for an example of using the onContextMenu<br />

event handler with a custom context menu.<br />

onCopy<br />

onCut<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓<br />

Example<br />

Listing 15-36 shows both the onBeforeCut and onCut event handlers in action<br />

(as well as onBeforePaste and onPaste). Notice how the handleCut() function<br />

not only stuffs the selected word into the clipboardData object, but it also erases<br />

the selected text from the table cell element from where it came. If you replace the<br />

onBeforeCut and onCut event handlers with onBeforeCopy and onCopy (and<br />

change handleCut() to not eliminate the inner text of the event source element),<br />

the operation works with copy and paste instead of cut and paste. I demonstrate<br />

this later in the chapter in Listing 15-45.<br />

Listing 15-36: Cutting and Pasting under Script Control<br />

<br />

<br />

onBeforeCut and onCut Event Handlers<br />

<br />

TD {text-align:center}<br />

TH {text-decoration:underline}<br />

.blanks {text-decoration:underline}<br />

<br />

<br />

function selectWhole() {<br />

var obj = window.event.srcElement<br />

var range = document.body.createTextRange()<br />

range.moveToElementText(obj)<br />

range.select()<br />

event.returnValue = false<br />

}<br />

function handleCut() {<br />

var rng = document.selection.createRange()<br />

clipboardData.setData(“Text”,rng.text)<br />

var elem = event.srcElement<br />

Continued<br />

101<br />

elementObject.onCopy

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

Saved successfully!

Ooh no, something went wrong!