一个禁止flash右键的方法



在做一个实验时遇到一个禁止flash的右键功能的问题。在网上找了很多方法,要不就完全不行,要不就浏览器不兼容。Google了一下 国外的情况,终于找到了一个不错的方法。该方法用了javascript和as的结合,完美解决了问题。

以下是javascript部分代码:


   1. var RightClick = {
   2.     /**
   3.      *  Constructor
   4.      */ 
   5.     init: function () {
   6.         this.FlashObjectID = "customRightClick";
   7.         this.FlashContainerID = "flashcontent";
   8.         this.Cache = this.FlashObjectID;
   9.         if(window.addEventListener){
  10.              window.addEventListener("mousedown", this.onGeckoMouse(), true);
  11.         } else {
  12.             document.getElementById(this.FlashContainerID).onmouseup = function() { document.getElementById(RightClick.FlashContainerID).releaseCapture(); }
  13.             document.oncontextmenu = function(){ if(window.event.srcElement.id == RightClick.FlashObjectID) { return false; } else { RightClick.Cache = "nan"; }}
  14.             document.getElementById(this.FlashContainerID).onmousedown = RightClick.onIEMouse;
  15.         }
  16.     },
  17.     /**
  18.      * GECKO / WEBKIT event overkill
  19.      * @param {Object} eventObject
  20.      */
  21.     killEvents: function(eventObject) {
  22.         if(eventObject) {
  23.             if (eventObject.stopPropagation) eventObject.stopPropagation();
  24.             if (eventObject.preventDefault) eventObject.preventDefault();
  25.             if (eventObject.preventCapture) eventObject.preventCapture();
  26.          if (eventObject.preventBubble) eventObject.preventBubble();
  27.         }
  28.     },
  29.     /**
  30.      * GECKO / WEBKIT call right click
  31.      * @param {Object} ev
  32.      */
  33.     onGeckoMouse: function(ev) {
  34.         return function(ev) {
  35.         if (ev.button != 0) {
  36.             RightClick.killEvents(ev);
  37.             if(ev.target.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {
  38.                 RightClick.call();
  39.             }
  40.             RightClick.Cache = ev.target.id;
  41.         }
  42.       }
  43.     },
  44.     /**
  45.      * IE call right click
  46.      * @param {Object} ev
  47.      */
  48.     onIEMouse: function() {
  49.         if (event.button> 1) {
  50.             if(window.event.srcElement.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {
  51.                 RightClick.call(); 
  52.             }
  53.             document.getElementById(RightClick.FlashContainerID).setCapture();
  54.             if(window.event.srcElement.id)
  55.             RightClick.Cache = window.event.srcElement.id;
  56.         }
  57.     },
  58.     /**
  59.      * Main call to Flash External Interface
  60.      */
  61.     call: function() {
  62.         document.getElementById(this.FlashObjectID).rightClick();
  63.     }
  64. }  

var RightClick = {      /**       *  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) { return false; } else { RightClick.Cache = "nan"; }}              document.getElementById(this.FlashContainerID).onmousedown = RightClick.onIEMouse;          }      },      /**       * GECKO / WEBKIT event overkill       * @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 / WEBKIT call right click       * @param {Object} ev       */      onGeckoMouse: function(ev) {          return function(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;          }        }      },      /**       * IE call right click       * @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;          }      },      /**       * Main call to Flash External Interface       */      call: function() {          document.getElementById(this.FlashObjectID).rightClick();      }  }

以下是as部分代码:


   1. package {
   2.     
   3.     import flash.display.*;
   4.     import flash.external.ExternalInterface;
   5.  
   6.     public classclass RightClick extends Sprite
   7.     {
   8.         
   9.         public function RightClick()
  10.         {
  11.             stage.scaleMode = StageScaleMode.NO_SCALE;
  12.             stage.align = StageAlign.TOP_LEFT;
  13.             
  14.             var methodName:String = "rightClick";
  15.             var method:Function = onRightClick;
  16.             ExternalInterface.addCallback(methodName, method);
  17.         }
  18.         
  19.         private function onRightClick():void {
  20.  
  21.             var mx:int = stage.mouseX;
  22.             var my:int = stage.mouseY;
  23.  
  24.             if(my> 0 && my <stage.stageHeight && mx> 0 && mx <stage.stageWidth) {
  25.                 // YOUR CODE HERE
  26.             }
  27.         }
  28.     }
  29. }  

你可能感兴趣的:(JavaScript,cache,IE,Flash,webkit)