右键单击重新加载页面,但没有上下文菜单

时间:2011-10-22 02:31:33

标签: javascript html web-applications webpage

有一个webapp,我想在右键单击时重新加载页面,以及该行为,我不希望显示上下文菜单。

3 个答案:

答案 0 :(得分:0)

你正在寻找这样的东西:

 function click(e) {
  if (navigator.appName == 'Netscape' && e.which == 3) {
   window.location.reload();
   return false;
  } else {
   if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2)
    window.location.reload();
    return false;
   }
   return true;
  }
 document.onmousedown=click;
 document.oncontextmenu=new Function("window.location.reload();return false;"); // line added

除了重新加载,您可以使用:

 history.go(0);
 window.location.href=window.location.href;

答案 1 :(得分:0)

最短的方法可能是拦截body标签中的右键单击事件,如下所示:

<html>
    <body oncontextmenu="window.location.reload(); return false;" onload="alert('loaded');">
        Testing 1 2 3 ...
    </body>
</html>

“return false”会阻止默认的contextmenu弹出。

编写自己的事件处理函数会让你获得更多控制权。

答案 2 :(得分:0)

<div id="nocontext">No context.</div>

window.onload = function(){
    document.getElementById('nocontext').oncontextmenu = function(){
        history.go(0);
        return false;
    }
}

注意:我复制了Jared Farrish在他的jsfiddle上发布的内容以及James提到的使用history.go(0)的内容;我在Explorer 9,Firefox和Safari中测试了以上脚本。

相关问题