javascript和as的结合解决禁止flash的右键功能
作者 佚名技术
来源 服务器技术
浏览
发布时间 2012-07-07
在做一个实验时遇到一个禁止Flash的右键功能的问题。在网上找了很多方法,要不就完全不行,要不就浏览器不兼容。Google了一下国外的情况,终于找到了一个不错的方法。该方法用了JavaScript和as的结合,完美解决了问题。 以下是Javascript部分代码: varRightClick={ /** *Constructor */ init:function(){ this.FlashObjectID="customRightClick"; this.FlashContainerID="flashcontent"; this.Cache=this.FlashObjectID; if(window.addEventListener){ window.addEventListener("mousedown",this.onGeckoMouse(),true); }else{ document.getElementById(this.FlashContainerID).onmouseup=function(){document.getElementById(RightClick.FlashContainerID).releaseCapture();} document.oncontextmenu=function(){if(window.event.srcElement.id==RightClick.FlashObjectID){returnfalse;}else{RightClick.Cache="nan";}} document.getElementById(this.FlashContainerID).onmousedown=RightClick.onIEMouse; } }, /** *GECKO/WEBKITeventoverkill *@param{Object}eventObject */ killEvents:function(eventObject){ if(eventObject){ if(eventObject.stopPropagation)eventObject.stopPropagation(); if(eventObject.preventDefault)eventObject.preventDefault(); if(eventObject.preventCapture)eventObject.preventCapture(); if(eventObject.preventBubble)eventObject.preventBubble(); } }, /** *GECKO/WEBKITcallrightclick *@param{Object}ev */ onGeckoMouse:function(ev){ returnfunction(ev){ if(ev.button!=0){ RightClick.killEvents(ev); if(ev.target.id==RightClick.FlashObjectID&&RightClick.Cache==RightClick.FlashObjectID){ RightClick.call(); } RightClick.Cache=ev.target.id; } } }, /** *IEcallrightclick *@param{Object}ev */ onIEMouse:function(){ if(event.button>1){ if(window.event.srcElement.id==RightClick.FlashObjectID&&RightClick.Cache==RightClick.FlashObjectID){ RightClick.call(); } document.getElementById(RightClick.FlashContainerID).setCapture(); if(window.event.srcElement.id) RightClick.Cache=window.event.srcElement.id; } }, /** *MaincalltoFlashExternalInterface */ call:function(){ document.getElementById(this.FlashObjectID).rightClick(); } } 以下是as部分代码: package{ importflash.display.*; importflash.external.ExternalInterface; publicclassclassRightClickextendsSprite { publicfunctionRightClick() { stage.scaleMode=StageScaleMode.NO_SCALE; stage.align=StageAlign.TOP_LEFT; varmethodName:String="rightClick"; varmethod:Function=onRightClick; ExternalInterface.addCallback(methodName,method); } privatefunctiononRightClick():void{ varmx:int=stage.mouseX; varmy:int=stage.mouseY; if(my>0&&my<stage.stageHeight&&mx>0&&mx<stage.stageWidth){ //YOURCODEHERE } } } } 支持的浏览器有: WINDOWS Vista Internet Explorer 7.0.6001 (16549) Firefox 2.0.0.6 (with mouse gestures disabled) Maxthon 2 (with mouse gestures disabled) Safari 3.0.3 (522.15.5) Windows XP SP2 Internet Explorer 6 Internet Explorer 7 Maxthon 2 (with mouse gestures disabled) FireFox 2 (with mouse gestures disabled) Safari 3 Netscap |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于javascript和as的结合解决禁止flash的右键功能的所有评论