A recent project requires JavaScript to automate the copying of text to the Clipboard, but only ie6.0 support is found when testing.
Baidu Search after found that the original ie7.0, Firefox is not support such operations, and then search for a bit, to find a solution:
Use JavaScript to call Flash to copy text to the Clipboard.
Things to note in HTML code:
In the object tag, insert id= "Fcopy"
In the Embed tab, insert Name= "fcopy" and swliveconnect= "true" to ensure that the id attribute is not used!
Flash Code:
// Import Externalinterface class Import Flash.external.ExternalInterface; function Copy (str) {system.setclipboard (str); GetURL ("Javascript:alert (' copied, use Ctrl + V paste out ')");
Allow Flalert () in JS to call Showalert () in Flash
Externalinterface.addcallback ("Copy", null,copy);
HTML code:
<ObjectClassID= "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"codebase= "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"ID= "Fcopy"width= "1"Height= "1"><paramname= "movie"value= "flash/clipboard.swf"><paramname= "Quality"value= "High"><paramname= "Swliveconnect"value= "true"><Embedsrc= "flash/clipboard.swf"width= "1"Height= "1"Quality= "High"pluginspage= "Http://www.macromedia.com/go/getflashplayer"type= "Application/x-shockwave-flash"name= "Fcopy"Swliveconnect= "true"></Embed></Object>
JS Code:
varBrowser =NewObject ();functionGetbrowser () {varb =navigator.userAgent.toLowerCase (); browser={safari:/webkit/. Test (b), Opera:/opera/. Test (b), IE6:/msie 6/.test (b) &&!/opera/. Test (b), IE7:/msie 7/.test (b) &&!/opera/. Test (b), MSIE:/msie/.test (b) &&!/opera/. Test (b), Mozilla:/mozilla/.test (b) &&!/(Compatible|webkit)/. Test (b)};} Getbrowser ();//Copy to clipboardfunctioncopycode (field) {Field.select ();vartext=Field.value;if(browser.ie6) {Window.clipboardData.setData (' Text ', Text) alert (' Copied, please use CTRL + V to paste it out ') ;}Else{flashCopy (text);//copy using Flash mode}}//call the method in Flash, "Fcopy" is the ID of the SWF in the HTML pagefunctionflashCopy (text) {Thismovie ("Fcopy"). copy (text);}//build JS and flash interoperability EnvironmentfunctionThismovie (moviename) {if(Navigator.appName.indexOf ("Microsoft")! =-1) {returnWindow[moviename]}Else{returnDocument[moviename]}}
Flash interacts with JavaScript