屏蔽Flash右键信息的js代码

作者:拾光

时间:2026-10-24

浏览:460

本文介绍如何通过JavaScript代码屏蔽Flash对象的右键菜单。代码兼容IE、Firefox和Chrome浏览器,利用addEventListener和attachEvent处理鼠标事件,阻止默认右键行为。示例展示了使用SWFObject嵌入Flash并调用屏蔽函数的完整流程,适用于需要禁用Flash右键操作的场景。

文章目录

屏蔽Flash右键菜单的JavaScript实现

以下是实现屏蔽Flash右键菜单的JavaScript代码示例:

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Yimin</title>
<script type="text/javascript">
function NoRightClick(pid){
//pid:flash's parentNode id
var el = document.getElementById(pid);
if(el.addEventListener){
el.addEventListener("mousedown",function(event){
if(event.button == 2){
event.stopPropagation(); //for firefox
event.preventDefault(); //for chrome
} 
},true);
}else{
el.attachEvent("onmousedown",function(){
if(event.button == 2){
el.setCapture();
} 
}); 
el.attachEvent("onmouseup",function(){
el.releaseCapture();
}); 
el.oncontextmenu = function(){
return false;
}; 
} 
}; 
</script>
</head>
<body>
<div id="testContent" style="width:800px">
<embed id="t1" height="780px" width="750" pluginspage="http://www.adobe.com/go/getflashplayer" wmode="opaque" menu="false" allowscriptaccess="sameDomain" algin="middle" name="t1" quality="high" bgcolor="#000000" style="" src="test.swf" type="application/x-shockwave-flash"/>
</div>
<script type="text/javascript">
var so = new SWFObject("test.swf", "t1", "800", "550", "9", "#000000");
so.addParam("quality", "high");
so.addParam("name", "t1");
so.addParam("id", "t1");
so.addParam("algin", "middle");
so.addParam("AllowScriptAccess", "sameDomain");
so.addParam("menu", "false");
so.addParam("wmode", "opaque");
so.addParam("pluginspage", "http://www.adobe.com/go/getflashplayer");
so.write("testContent");
NoRightClick("testContent");
</script>
</body>
</html>

经过实际测试,该代码能够在IE、Firefox以及Google Chrome浏览器中成功移除Flash的右键菜单,操作简便且效果良好。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章