The oncopy Event


The oncopy Event

The oncopy event occurs when the user copies an object or a selection (as with the browser's Edit Copy menu item or by pressing Ctrl+C in Windows), adding it to the system clipboard. You can find the support for this event in Table 6.64.

Table 6.64. The oncopy Event

Event

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

oncopy

             

x

x

x

In the Internet Explorer, the window object has a clipboardData object with setData and getData methods that enable you to handle clipboard data. You pass setData the data format ( "Text" or "URL" ) and the data to place in the clipboard. You pass getData the format of the data you want, and it returns the data.

Here's an example using oncopy and onpaste : When the user copies text from a <SPAN> element, we actually cancel the normal copy operation (by setting event.returnValue to false) and place our own text in the clipboard (which enables us to stop the user from copying sensitive dataassuming the user has not deactivated scripting in the browser). When the user pastes the text into a text field, we handle that operation ourselves as well:

(Listing 06-13.html on the web site)
 <HTML>      <HEAD>          <TITLE>Using the Clipboard</TITLE>          <SCRIPT LANGUAGE="JavaScript">              <!--             function copy()              {  window.clipboardData.setData("Text", "Hello from JavaScript!")   event.returnValue = false  }              function paste()              {  text1.value = window.clipboardData.getData("Text")   event.returnValue = false  }              // -->          </SCRIPT>      </HEAD>      <BODY>          <H1>Using the Clipboard</H1>          <SPAN oncopy="copy()">Here is some text to copy.</SPAN>          <BR>          <INPUT ID="text1" onpaste="paste()">      </BODY>  </HTML> 

You can see the results in Figure 6.8, where no matter what text you copy from the <SPAN> element, you'll see Hello from JavaScript! when you paste the text into the text field.

Figure 6.8. Using the clipboard.

graphics/06fig08.gif



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net