如何防止iframe中的上下文菜单? [英] How to prevent context menu in an iframe?

查看:113
本文介绍了如何防止iframe中的上下文菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图阻止浏览器的上下文菜单显示在每个元素'iframe'的事件上'右键单击',但它不工作 - 上下文菜单仍在工作

这里是我的代码:

iframe)。each(function(){$(this).on(contextmenu,function(e){e.preventDefault(); // return false;});});});

 < script src =https://ajax.googleapis.com /ajax/libs/jquery/2.1.1/jquery.min.js\"></script><iframe src =http://www.google.com>< / iframe>  

在浏览器中是不同的安全上下文和每个人都从另一个沙箱中。这是为了防止一个页面能够篡改另一个页面。



因此,iframe上的样式,脚本和脚本访问仅限于iframe元素本身,而不是任何其内容。同样,出于同样的安全原因,iframe无法访问它的父级。



防止iframe中的上下文菜单的唯一方法是将上下文菜单防护脚本加载到iframe本身。如果您无法访问iframe的内容,那么这是不可能的。如果是这样,您可能会干扰第三方网站在iframe内部的运作。


I am trying to prevent browsers's context menu from showing on the event 'Right click' on every element 'iframe' but it isn't working -- the context menu is still working
here is my code:

$(document).ready(function () {
       $("iframe").each(function(){
           $(this).on("contextmenu",function(e){
               e.preventDefault(); //return false;
           });
       });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<iframe src="http://www.google.com"></iframe>

解决方案

Each window, tab and iframe in a browser are different security contexts and each is sandboxed from the other. This is to prevent one page from being able to tamper with another.

So styles, script and script access on an iframe are limited to the iframe element itself, not any of its contents. Similarly, an iframe cannot access it's parent for the same security reasons.

The only way to prevent the context menu inside an iframe is to load your context menu prevention script inside the iframe itself. If you don't have access to the iframe's contents, then it is not possible. If it were, you would be potentially interfering with how a 3rd party website operates inside that iframe.

这篇关于如何防止iframe中的上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆