Compatible with the mainstream browser JS copy content to the Clipboard _javascript tips

Source: Internet
Author: User

Now more and more browsers, such as IE, Firefox, Chrome, Safari and so on, so now to achieve a JS copy content to the Clipboard small function is not so easy.

in the Flash 9 era, there is a program that kills all of the browser's JS copy content to the Clipboard :

This program is one of the most popular methods: the famous clipboard copy solution utilizes a clipboard.swf as a bridge to copy content to the Clipboard.

The principle is: Create a hidden Flash file, and give the Flash variable flashvars assignment "clipboard= ...", through this assignment flash will put the copied content to the Clipboard. This method is compatible with IE, Firefox, Opera, Chrome, Safari, and is really a "panacea" solution. The installation rate of the browser flash is very high, which is almost a perfect solution.

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>web Developer-Www.Admin10000.com </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<script type= "Text/javascript" >
var clipboardswfdata;
var setcopy_gettext = function () {
Clipboardswfdata = document.getElementById (' Test_text '). Value;
alert (clipboardswfdata);
Window.document.clipboardswf.SetVariable (' str ', clipboardswfdata);
}
var Floatwin = function () {
Alert (' Copy successful! ');
document.getElementById (' Clipinner '). style.display = ' None ';
}
</script>
<body>
<textarea id= "Test_text" rows= "cols=" > Text content .......</textarea>
<div id= "Clipboard_content" >
<div class= "My_clip_button" ><span class= "Clipinner" id= "Clipinner" > Copy code to the Clipboard
<embed name= "clipboardswf" class= clipboardswf "id=" clipboardswf "onmouseover=" Setcopy_gettext () "Devicefont=" False "src="./_clipboard.swf "menu= false" allowscriptaccess= "Samedomain" swliveconnect= "true" wmode= "Transparent" Type= "Application/x-shockwave-flash" height= "width=" >
</span>
</div>
</div>
</body>

CLIPBOARD.SWF's download Address: http://www.jeffothy.com/weblog/uploads/clipboard.php

But in the Flash 10 era, the above methods have been exhausted.

Because the flash10 that only the real operations on the SWF (such as mouse clicks) to access the Clipboard, and this method is only a hidden SWF file, through JavaScript operation Flash Clipboard, the user does not have the SWF file for real operation, So this method is ineffective.

So how to solve the problem of "real operation"? You can use a JavaScript library: Zero Clipboard, which enables you to use the Flash 10 implementation to copy to the Clipboard. The idea is to overwrite a DOM element such as a button or div on a transparent flash (invisible to the user), and when clicked on the DOM, it actually clicks on Flash, which accesses the Flash clipboard.

Here's a good example of debugging:

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>zero Clipboard test</title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<script type= "Text/javascript" src= "Zeroclipboard.js" ></script>
<script type= "Text/javascript" >
var clip = null;
function $ (ID) {return document.getElementById (ID);}
function init () {
Clip = new Zeroclipboard.client ();
Clip.sethandcursor (TRUE);
Clip.addeventlistener (' MouseOver ', function (client) {
Update the text on mouse over
Clip.settext ($ (' Fe_text '). Value);
});

Clip.addeventlistener (' Complete ', function (client, text) {
Debugstr ("Copied Text to Clipboard:" + text);
Alert ("The address has been copied and you can paste it using CTRL + V.") ");
});

Clip.glue (' Clip_button ', ' Clip_container ');
}
</script>
<body onload= "init ()" >
<input id= "Fe_text" cols= "rows=" "5" value= "Copy content text" >
<span id= "Clip_container" ><span id= "Clip_button" ><strong> replication </strong></span></ Span>
</body>

Click to download this class library: http://www.jb51.net/jiaoben/24961.html

When debugging, please upload to the website, the local direct open flash will be wrong, no permissions. The Moviepath attribute in the Zeroclipboard.js file is the Falsh address, which is the location of the address in the directory where the zeroclipboard.swf resides.

This JS copy content to the Clipboard program can support the browser: Firefox/ie/opera/chorme/safari all browsers!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.