Copy the code

Reprinted from: offspring

 

But IE6 two lines of JS code can, IE7 is almost over, but out of a security prompt more disgusting, if users see, there must be doubts;
and then but that Firefox, Chrome, etc. simply do not allow you to copy;

remember online this aspect of the code, looking for a bit and found that all can not be, and finally found one more than firefox3.5 version, the code is also very, very complicated, do not dare to use.
Finally, there is no way to hold back, and checked to find information, write their own one:

several major points to understand:
1, the Firefox browser, for security reasons it directly is not to copy;
2, Flash, you can use System.setClipboard (), threw the contents of the clipboard, and then let FLASH work in Firefox;
3, after the Flash player 10.0 does, it is for security reasons, the content of System.setClipboard must FLASH inside;
4, with ExternalInterface can communicate JS;
. 5, ExternalInterface be incorporated in what can flash8;

The default HTML code:

  1. <input type="text" id="testInput" name="testInput" value="4234324234" />  
  2. <div id="buttonBox">  
  3.     <button onclick="copy('testInput')">copy</button>  
  4. </div>  


Therefore, when the design, to make the first determination, if it is IEs, stick with the default code, so there is no problem most, if not covered, with a FLASH to the default buttons;

  1. if (window.XMLHttpRequest) {// If not IE, to use FLASH manner replication  
  2.     $('buttonBox').innerHTML = '<embed src="flashCopy.swf" width="48"   
  3. height="23" quality="high"    
  4. pluginspage="http://www.macromedia.com/go/getflashplayer"   
  5. type="application/x-shockwave-flash"></embed>';  
  6. }  


Here are all the JS file:

  1. <script>  
  2. function $(id){  
  3.     return document.getElementById(id);  
  4. }  
  5.   
  6. function copy(){//ie6  
  7.     var value = $('testInput').value;    
  8.     window.clipboardData.clearData();     
  9.     window.clipboardData.setData("Text", value);   
  10.     alert ( 'copied successfully!');   
  11. }  
  12.   
  13. function flashCopy(){//firefox .......  
  14.     return $('testInput').value;    
  15. }  
  16.   
  17. function flashCopyBack(){  
  18.     alert ( 'copied successfully!');  
  19. }  
  20.   
  21. if ( "v"! = "v") {// If not IE, to use FLASH manner replication  
  22.     $('buttonBox').innerHTML = '<embed src="111.swf" width="48" height="23" quality="high"  pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>';  
  23. }  
  24. </script>  


FLASH button code is as follows:

  1. on (release) {  
  2.     import flash.external.ExternalInterface;  
  3.     var inputText = ExternalInterface.call('flashCopy');  
  4.     System.setClipboard(inputText);  
  5.     ExternalInterface.call('flashCopyBack');  
  6.     //_root.boboText.text = inputText;  
  7. }  


Principle, is to avoid the security restrictions that, at the point when the button FLASH in through the FLASH code to tune JS code page, JS code can get the value of INPUT, then pass FLASH, then , FLASH these values in there, and then, FLASH and through their own deposit System.setClipboard these values to the clipboard; then again, he again invoked the page flashCopyBack, flashCopyBack only do one thing, that has prompted copy successful!

I am in Firefox, chrome, ie are measured before, no problem, if anyone found to have problems, please tell me, thank you, because I have now started! 

Please indicate the source: offspring

Guess you like

Origin blog.csdn.net/soliy/article/details/5594195