Home Web Front-end H5 Tutorial HTML5 actual combat and analysis of clipboard events

HTML5 actual combat and analysis of clipboard events

Feb 11, 2017 am 11:34 AM

Many websites will display relevant prompts about the clipboard when copying and pasting. When I first started working as a front-end engineer, I was still wondering how this was achieved. Now, we no longer have to wonder, because browsers have already incorporated clipboard-related events into standards.

IE is the first browser to support clipboard-related events and access clipboard data through JavaScript. IE's implementation became the de facto standard, and subsequently Firefox 3+, Chrome, and Safari 2+ all supported similar events and clipboard access, but Opera did not support access to the clipboard through JavaScript. Until the arrival of HTML5, clipboard-related events were incorporated into the HTML5 specification. Below are the 6 clipboard events.

 beforecopy:Triggered before the copy operation occurs;

 copy:Triggered when the copy operation occurs;

 beforecut:Triggered before the cutting operation occurs;

 cut:Triggered when the cutting operation occurs;

 beforepaste: Triggered before the paste operation occurs;

 paste: Triggered when the paste operation occurs.

Since there is no standard for clipboard operations, these events and related objects will vary from browser to browser. In Firefox, Chrome, and Safari, the beforecopy, beforecut, and beforepaste events will only fire when the context menu for the text box is displayed (expecting a clipboard event). But IE will trigger these events before triggering the copy, cut, and paste events. As for the copy, cut and paste events, all browsers will trigger them as long as the corresponding option is selected in the context menu (right-click menu), or the corresponding keyboard key combination such as (ctrl+v) is used.

Before the actual event occurs, the beforecopy, beforecut and beforepaste events can be used to modify the data before sending data to the clipboard or getting data from the clipboard. However, canceling these events will not cancel the operation on the clipboard. Only canceling the copy, cut and paste events can prevent the corresponding operations from occurring.

To access the data in the clipboard, you can use the clipboardData object: in IE, the clipboardData object is a property of the window object; in Chrome, Safari and Firefox 4+, the clipboardData object is a property of the corresponding event pair . However, in Chrome, Safari, and Firefox 4+, the clipboardData object is only valid during handling of clipboard events. This is to prevent unauthorized access to the clipboard; in IE, the clipboardData object can be accessed at any time. To ensure cross-browser compatibility, it is best to only use this object during clipboard events.

This clipboardData object has three methods: getData() method, setData() method and clearData() method. Among them, the getData() method is used to obtain data from the clipboard. It receives a parameter, which is the data format to be obtained. In IE, there are two data formats: 'text' and 'URL'. In Chrome, Safari, and Firefox 4+, this parameter is a MIME type; however, 'text' can be used to represent 'text/plain'.

The first parameter of the setData() method is also the data type, and the second parameter is the text to be placed in the clipboard. For the first parameter, IE still supports "text" and "URL". , and in Chrome and Safari, the MIME type is still supported. However, unlike the getData() method, the setData() method in Chrome and Safari cannot recognize the "text" type successfully. Once it is in the clipboard, it will return true; otherwise, it will return false. Well, let’s look at the following small example.

//获取剪贴板数据方法
function getClipboardText(event){	var clipboardData = event.clipboardData || window.clipboardData;	return clipboardData.getData("text");
};


//设置剪贴板数据
function setClipboardText(event, value){	if(event.clipboardData){		
return event.clipboardData.setData("text/plain", value);	
}else if(window.clipboardData){		
return window.clipboardData.setData("text", value);	}
};
Copy after login
The getClipboardText() method here is relatively simple. It only needs to determine the position of the clipboardData object, and then call the getData() method with the "text" type. Correspondingly, setClipboardText. () method is a little more complicated. After obtaining the clipboardData object, you need to pass in different types for the setData() method according to different browser implementations (for Chrome and Safari, it is "text/plain"; for IE, it is "text"). Having access to the clipboard is useful when you need to ensure that the text you paste into a text box contains certain characters, or conforms to a certain format. For example, if a text box only accepts numeric values, then You must check the pasted value to ensure it is valid. In the paste event, you can determine whether the value in the clipboard is valid. If it is invalid, you can cancel the default behavior as in the following example. HTML code

<input type="text" id="inp" value="梦龙小站" />
Copy after login

 

JavaScript code

window.onload = function(){
	var oInp = document.getElementById("inp");

	function getClipboardText(event){
		var clipboardData = event.clipboardData || window.clipboardData;
		return clipboardData.getData("text");
	};

	oInp.addEventListener(&#39;paste&#39;,function(event){
		var event = event || window.event;
		var text = getClipboardText(event);

		if(!/^\d+$/.test(text)){
			event.preventDefault();
		}
	}, false);	
}
Copy after login

Here, the onpaste event handler ensures that only numeric values ​​are pasted into the text box if the clipboard value does not match the regular expression. , the paste operation will be canceled. Chrome, Firefox and Safari only allow access to the getData() method in the onpaste event handler .

Since not all browsers support access to the clipboard, a simpler method is to block one or more clipboard operations. In browsers that support copy, cut, and paste events (Firefox 3+, Safari, Chrome, and IE), it is easy to prevent the default behavior of these events. In Opera, you need to prevent keystrokes that trigger these events, and also prevent the context menu (right-click menu) from being displayed in the text box.

Although clipboard-related events have been included in the HTML5 specification, Opera still does not support clipboard-related events, so practical application is still a headache. This concludes the introduction to the clipboard event of HTML5 actual combat and analysis. For more relevant knowledge about HTML5, please pay attention to the relevant updates of Menglong Station.

The above is the content of HTML5 actual combat and analysis of clipboard events. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!




Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

See all articles