禁用页面及页面所有frame内的右键菜单

   今天遇到一个需求,需要禁用页面的右键菜单,按照网上搜索的结果进行设置后发现单独一个页面可以,里面有嵌套的frame的话不对frame内的区域起作用。经过翻阅文档,自己写了一段脚本,测试完美通过。

a.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>
<script>

//禁用所有子frame中的右键
function noRight(doc){
//frames的集合
	var fs = doc.frames;
	for(var i=0;i<fs.length;i++){
		var f = fs[i];
		var d = f.document;
		var b = d.body;
	
		b.oncontextmenu=bodyNoRight;
		noRight(d);
	}
}

function bodyNoRight(){
	return false;
}
</script>

 <BODY oncontextmenu="return false" onload="noRight(this.document)">

  aaaaaaaa

  <iframe src="b.html"></iframe>
 </BODY>
</HTML>

 b.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document--b </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
  bbbbbbbbbbbbbb
  <iframe src="c.html"/>
 </BODY>
</HTML>

 

c.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document---c </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
  cccccccccccccccccccc
 </BODY>
</HTML>

 

你可能感兴趣的:(js,frame,禁用右键)