Javascript 右键单击重新加载页面,但没有上下文菜单
有一个Web应用程序,我想右键单击重新加载页面,同时我不想显示上下文菜单。您正在寻找这样的内容:Javascript 右键单击重新加载页面,但没有上下文菜单,javascript,html,web-applications,webpage,Javascript,Html,Web Applications,Webpage,有一个Web应用程序,我想右键单击重新加载页面,同时我不想显示上下文菜单。您正在寻找这样的内容: function click(e) { if (navigator.appName == 'Netscape' && e.which == 3) { window.location.reload(); return false; } else { if (navigator.appName == 'Microsoft Internet Explorer'
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;
最短的方法可能是拦截body标记中的右键单击事件,如下所示:
<html>
<body oncontextmenu="window.location.reload(); return false;" onload="alert('loaded');">
Testing 1 2 3 ...
</body>
</html>
return false防止弹出默认的contextmenu
不过,编写自己的事件处理程序函数将为您提供更多的控制
<div id="nocontext">No context.</div>
window.onload = function(){
document.getElementById('nocontext').oncontextmenu = function(){
history.go(0);
return false;
}
}
注意:我复制了Jared Farrish在他的JSFIDLE上发布的内容以及James提到的关于使用history.go0的内容;我在Explorer 9、Firefox和Safari中测试了上述脚本。页面正在重新加载,但是上下文菜单呢?我不想那样。还有一件事,我在页面上有一个textarea字段,不允许我写东西,但我可以在IE中做。这会在启动和重新加载页面之前捕获上下文菜单。直到文本区域。如果没有看到代码或测试页面,我无法告诉您。请看,我正在使用Google Chrome,上下文菜单仍然存在。需要在脚本底部添加以下行。它还将禁用文本区域中的右键单击。document.oncontextmenu=新函数window.location.reload;返回错误;;