哈,之前那篇转来的博文实现这个功能实在太复杂而且兼容性很有问题。所以去了很多E文网站上搜索,终于发现一个又简便兼容性又好的方法,总结一下:
index.template.html:
<!-- saved from url=(0014)about:internet -->
<html lang="en">
<!--
Smart developers always View Source.
This application was built using Adobe Flex, an open source framework
for building rich Internet applications that get delivered via the
Flash Player or to desktops via Adobe AIR.
Learn more about Flex at http://flex.org
// -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- BEGIN Browser History required section -->
<link rel="stylesheet" type="text/css" href="history/history.css" />
<!-- END Browser History required section -->
<title>${title}</title>
<script src="AC_OETags.js" language="javascript"></script>
<!-- BEGIN Browser History required section -->
<script src="history/history.js" language="javascript"></script>
<!-- END Browser History required section -->
<style>
body { margin: 0px; overflow:hidden }
</style>
<script language="JavaScript" type="text/javascript">
<!--
// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = ${version_major};
// Minor version of Flash required
var requiredMinorVersion = ${version_minor};
// Minor version of Flash required
var requiredRevision = ${version_revision};
// -----------------------------------------------------------------------------
var RightClick = {
init: function () {
this.FlashObjectID = "RightClickDemo";
this.FlashContainerID = "flashcontent";
this.Cache = this.FlashObjectID;
if(window.addEventListener){
window.addEventListener("mousedown", this.onGeckoMouse(), true);
document.oncontextmenu = function() { document.getElementById("RightClickDemo").rightClick(); }
} 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;
}
},
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(); }
}
},
onGeckoMouse: function(ev) {
return function(ev) {
if (ev.button != 0) {
RightClick.killEvents(ev);
if(ev.target.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {
document.getElementById(RightClick.FlashObjectID).rightClick();
}
RightClick.Cache = ev.target.id;
}
}
},
onIEMouse: function() {
if (document.getElementById(RightClick.FlashObjectID + 'x'))
document.getElementById(RightClick.FlashObjectID + 'x').id = RightClick.FlashObjectID;
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;
}
},
call: function() {
document.getElementById(RightClick.FlashObjectID).rightClick();
}
}
// -->
</script>
</head>
<body scroll="no" onLoad="RightClick.init();">
<div id="flashcontent">
<script language="JavaScript" type="text/javascript">
<!--
// Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
var hasProductInstall = DetectFlashVer(6, 0, 65);
// Version check based upon the values defined in globals
var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
if ( hasProductInstall && !hasRequestedVersion ) {
// DO NOT MODIFY THE FOLLOWING FOUR LINES
// Location visited after installation is complete if installation is required
var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
var MMredirectURL = window.location;
document.title = document.title.slice(0, 47) + " - Flash Player Installation";
var MMdoctitle = document.title;
AC_FL_RunContent(
"src", "playerProductInstall",
"FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
"width", "${width}",
"height", "${height}",
"align", "middle",
"id", "${application}",
"quality", "high",
"bgcolor", "${bgcolor}",
"name", "${application}",
"wmode","opaque",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);
} else if (hasRequestedVersion) {
// if we've detected an acceptable version
// embed the Flash Content SWF when all tests are passed
AC_FL_RunContent(
"src", "${swf}",
"width", "${width}",
"height", "${height}",
"align", "middle",
"id", "${application}",
"quality", "high",
"bgcolor", "${bgcolor}",
"name", "${application}",
"wmode","opaque",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);
} else { // flash is too old or we can't detect the plugin
var alternateContent = 'Alternate HTML content should be placed here. '
+ 'This content requires the Adobe Flash Player. '
+ '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>';
document.write(alternateContent); // insert non-flash content
}
// -->
</script>
<noscript>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
id="${application}" width="${width}" height="${height}"
codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
<param name="movie" value="${swf}.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="${bgcolor}" />
<param name="allowScriptAccess" value="sameDomain" />
<embed src="${swf}.swf" quality="high" bgcolor="${bgcolor}"
width="${width}" height="${height}" name="${application}" align="middle"
play="true"
menu="false"
wmode="opaque"
loop="false"
quality="high"
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage="http://www.adobe.com/go/getflashplayer">
</embed>
</object>
</noscript>
</div>
</body>
</html>
RightClickDemo.as
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()" click="onClick(event)" viewSourceURL="srcview/index.html" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#320000, #000000]">
<mx:Script>
<![CDATA[
private function init() : void
{
ExternalInterface.addCallback("rightClick", onRightClick);
}
private function onRightClick():void
{
var mx:int = stage.mouseX;
var my:int = stage.mouseY;
if(my> 0 && my < stage.stageHeight && mx> 0 && mx < stage.stageWidth)
{
this.testMenu.x = mx;
this.testMenu.y = my;
this.testMenu.visible = true;
}
}
private function onClick(event : MouseEvent) : void
{
this.testMenu.visible = false;
}
]]>
</mx:Script>
<mx:List x="0" y="0" width="150" id="testMenu" visible="false" editable="false" enabled="true" selectionColor="#ffffff">
<mx:dataProvider>
<mx:Array>
<mx:String>Item 1</mx:String>
<mx:String>Item 2</mx:String>
<mx:String>Item 3</mx:String>
<mx:String>Item 4</mx:String>
<mx:String>Item 5</mx:String>
<mx:String>Item 6</mx:String>
<mx:String>Item 7</mx:String>
</mx:Array>
</mx:dataProvider>
</mx:List>
<mx:Label x="500" y="10" text="RightClick anywhere inside this flash" alpha="0.5" color="#fff"/>
</mx:Application>