Right Click Event?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Right Click Event?

Post by spacebiscuit »

Hi guys,

I appreciate that this would require the use of some javascript, BUT.... would it be possibl to have an email to be sent when a right click is made on an HTML page.

Any help would be appreciated as I do not really have a clue where to start!

Thanks in advance,

Rob.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Right Click Event?

Post by omniuni »

Well, I can tell you where to start; oncontextmenu.

It's a javascript event that detects when right-clicks are pressed... sort of. Different browsers use different numbers for the mouse keys, so it's more reliable to detect when a context menu is shown, rather than when a certain button is pressed. (event.button for a right click is 2, and this MAY work in both FF and IE, but it is likely a less reliable method to determine what you want...)
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Right Click Event?

Post by spacebiscuit »

Ok this is where I am at present:

Code: Select all

<script language="javascript">
 function click(e) {
  if (event.button==2||evetbutton=-3') {
   alert("<?php myfunction() ?>");
   return true;
                                                         }
                         }
document.onmousedown=click
</script>
However when I load my page the 'yfunction' code is executing before I have pressed a button. It seems to be excuting opon the page load.

Any ideas?

Thanks,

Rob.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Right Click Event?

Post by omniuni »

It is triggering when the PHP function is called, which is when the script generates the HTML delivered to the browser. It is not triggered when the page is loaded, it is triggered when PHP is told to generate the page.

Javascript can not directly call PHP. You will need to either set up an AJAX object, or you can do something like have javascript open a popup (with a page to send the mail), wait 50ms, and close the popup. That would work, it would just be... sloppy.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Right Click Event?

Post by spacebiscuit »

Ok that makes sense, php being server side and javascript client side.

I have implemented my code using the window open. how do I autoclose the windwos after a set period though? Also my code will only implment if the user aloows the popup. Is that ocrrect, I am guessing there is no way around this?

Thanks,

Rob.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Right Click Event?

Post by omniuni »

You can use an AJAX object request to call the script.

To set a delay:

setTimeout(window.close,delaytime)

should do the trick.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Right Click Event?

Post by spacebiscuit »

Will the AJAX object request by-pass the popup blocker?

Any tips regarding AJAX code, I've not used it before.

Thanks,

Rob.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Re: Right Click Event?

Post by Charles256 »

Rob,

Check out to help you learn some of the fundamentals http://www.ajaxf1.com/tutorial/ajax-php.html . Do some google on ajax with php tutorials to find more info.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Right Click Event?

Post by spacebiscuit »

Awesome thanks guys the AJAX work likea treat. I have it working as I wish except for the auto close. What is the name of the function that does this?

THANK YOU!

Rob.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Right Click Event?

Post by omniuni »

window.close()
Post Reply