Firefox下实现复制文本内容的方法
Posted in Java, Me, UED on 七月 16th, 2009 by corrie – 3 Comments刚做了一个网址,可以说是put8.com的升级版 http://5url.cn,需要一个复制按钮,用IE的不行,用有些朋友提供的flash版本的也不行,因为我不会写FLASH AS3,所以只能找资源。正好发现了http://bit.ly用的是flash版本的,经过用firebug分析。用闪客精灵反向解析了flash的代码,贴出来一部分供大家学习。
private function copyClick(param1:MouseEvent) : void
{
var e:* = param1;
try
{
this.copyText = ExternalInterface.call(“CopyClipboardButton.getCopyText”, this.copyTextContainerId);
trace(this.copyText);
trace(this.copyTextContainerId);
}// end try
catch (error:Error)
{
this.copyText = “”;
}// end catch
if (this.copyText == “” && this.copyTextOrig != “”)
{
this.copyText = this.copyTextOrig;
}// end if
System.setClipboard(this.copyText);
if (this.imageUrl == null)
{
tf.htmlText = “Copied!“;
}// end if
return;
}// end function
他用ExternalInterface.call方法调用了外部的方法,获得了值然后传回到FLASH执行copy。
就写了CopyClipboardButton.getCopyText方法测试
CopyClipboardButton = {};
CopyClipboardButton.getCopyText = function(){alert(‘text’);}
测试执行JS函数成功,修改函数成返回值的形式,通过Flashvars把调用的元素和图片定义好就OK了
var CopyClipboardButton={};
CopyClipboardButton.getCopyText=function(a){
var b=document.getElementById(a);
try{return(b.value||b.innerText||b.textContent)}catch(c){return”"}
};<object width=”61″ height=”30″>
<param name=”movie” value=”CopyClipboardButton.swf?v=3.0″/>
<param name=”FlashVars” value=”copyTextContainerId=copyTextValue&fontSize=14&fontFace=Helvetica&fontColor=#555555&imageUrl=./index_copy_button_bento.jpg©Text=”/>
<param name=”quality” value=”high”/>
<param name=”menu” value=”false”/>
<param name=”wmode” value=”transparent”/>
<embed width=”61″ height=”30″ type=”application/x-shockwave-flash” src=”CopyClipboardButton.swf?v=3.0″ flashvars=”copyTextContainerId=copyTextValue&fontSize=14&fontFace=Helvetica&fontColor=#555555&imageUrl=./index_copy_button_bento.jpg©Text=” wmode=”transparent”/>
</object>
一个Flash版本的复制按钮就OK了,FLASH AS3跟JS进行交互来实现一些功能的实例其实很多。
























































































