右键单击时重定向Javascript

Javascript redirect when right clicking

本文关键字:Javascript 重定向 单击 右键      更新时间:2023-09-26

当一个人右键单击时,你如何使javascript重定向?甚至可能吗?

    var message="MESSAGE TO USER"
    function clickIE4(){
if (event.button==2){
alert(message); 
window.open("site.com/whatever.php");
return false;
}
}
function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
window.open("site.com/whatever.php");
return false;
}
}
}
if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function("alert(message);return false")

我正在尝试将 window.open("site.com/whatever.php")插入脚本中,但没有成功。

编辑

我希望在警报框关闭时进行重定向。很抱歉之前没有说清楚。

更改

if (document.layers){
  document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
  document.onmousedown=clickIE4;
}

if (document.layers){
  document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
  document.onmousedown=clickIE4;
}
else { 
  document.onmousedown = function (e) {
  if (e.button == 2)
  {
      alert("Ouch! that hurts. Please do not right click!");
      setTimeout(function() { location.href="http://www.google.com/"; }, 500);
  }
 }
}

我认为您需要使用带有协议的完整URL:

window.open('http://www.google.com');

右键单击问题已在此处得到解答:

右键单击是Javascript事件吗?