0

How do you make javascript redirect when a person right clicks? Is it even possible?

    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")

I'm trying to insert the window.open("site.com/whatever.php"); into the script but having no success.

Edit

I want the redirect to take place when the alert box has been closed. Sorry for not making this clear before.

user2425108
  • 35
  • 1
  • 8

2 Answers2

0

I think you need to use the complete URL with protocol:

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

The rightclick issue has been answered here:

Is right click a Javascript event?

Community
  • 1
  • 1
  • exactly what you just said – user2425108 Sep 15 '13 at 22:25
  • So you're saying window.open('http://www.google.com'); does not work and there are no correct answers in the link I posted? –  Sep 15 '13 at 22:27
  • that's exaxctly what i'm saying. But appreciate the help. and I tried http://google.com. Like you said also. – user2425108 Sep 15 '13 at 22:28
  • It seems the right click mouse event doesn't open a new window. I can get it to open a new window while it's displaying the alert box, however this is not what I want. I want the redirect to take place straight after the alert box is shown (ie closed). That way a user has time to read it before they are auto redirected. – user2425108 Sep 15 '13 at 22:33
0

Change

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

to

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);
  }
 }
}
fahadash
  • 3,133
  • 1
  • 30
  • 59
  • Sorry if I didn't make this clear before. Your script works. But I want the redirect to take place after the alert box is shown. However if it cannot be done i'll just stick with this and add a delay on the redirect. – user2425108 Sep 15 '13 at 22:39
  • That works perfectly. Although I think a redirect in the same window would look alot better. Thanks – user2425108 Sep 15 '13 at 22:44
  • You're a star my friend ^^ – user2425108 Sep 15 '13 at 22:47
  • @user2425108 you should accept the answer by clicking the check if you think it is a good answer because it gives the answerer 15 reputation and lets people know that the question has been answered. – markasoftware Sep 15 '13 at 23:11