Jump to content


JavaScript Righ Click


3 replies to this topic

#1 Tarquin

    Young Padawan

  • Members
  • Pip
  • 67 posts
  • Gender:Male
  • Location:London
  • Interests:I'm a huge Crystal Palace fan (Soccer team in England for you Americans)

Posted 29 November 2006 - 02:37 PM

Hi there,

I am having to explain the JavaScript right click coding as part of my website coursework. However, I dont actually understand the coding as I just downloaded it (its ok I put the source in my coursework! :( )

But anyway, can someone help me with this by telling me what each line means?

I understand what this line means

var message="ENTER MESSAGE HERE";

And I also understand that the 'message' part after the variable also acts as a link back, but I dont understand this lot.

function click(e) {
if (document.all) {
if (event.button == 2) {
alert(message);
return false;
}
}
if (document.layers) {
if (e.which == 3) {
alert(message);
return false;
}
}
}
if (document.layers) {
document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown=click;
Thanks alot guys, you really have been a lot of help.

#2 Wolfe

    Young Padawan

  • Members
  • Pip
  • 102 posts
  • Gender:Male
  • Location:Louisiana

Posted 29 November 2006 - 04:04 PM

I'm a little shaky on javascript, but I did little research here on the matter to verify my explanation.

function click(e) {		   
if (document.all) {		  
if (event.button == 2) {	 // If the button pressed was a right click send alert
alert(message);		   
return false;
}
}
if (document.layers) {	  // I think this has the same function as the other if, but for Mac
if (e.which == 3) {			 
alert(message);
return false;
}
}
}
if (document.layers) {	// Another way to get mousedown event, helps for browser capability
document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown=click;

So, basically, the first two if conditionals both do the same thing, they're just two different ways to do the same thing because of browser capability issues.

And this part:
if (document.layers) {	// Another way to get mousedown event, helps for browser capability
document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown=click;

This is pretty much the same thing as for the above conditionals. Both do the same thing because of cross-browser issues.

The function tag wraps all of the above code into a function to make it execute all of the code on the mousedown event.

Because doing onmousedown and putting tons of code.. just doesn't work; hence, the function.

Edited by Wolfe, 29 November 2006 - 04:07 PM.


#3 Tarquin

    Young Padawan

  • Members
  • Pip
  • 67 posts
  • Gender:Male
  • Location:London
  • Interests:I'm a huge Crystal Palace fan (Soccer team in England for you Americans)

Posted 29 November 2006 - 06:18 PM

Ahhh I thought the '(event.button == 2)' bit was to do with the right click, thats what I said to my teacher, as im guessing that if it was '(event.button == 1)' That is left click?


Actually I can test this out.

Thanks for your help ;)

#4 Wolfe

    Young Padawan

  • Members
  • Pip
  • 102 posts
  • Gender:Male
  • Location:Louisiana

Posted 29 November 2006 - 07:19 PM

The '(event.buttom == 2)' is related to the right click. It's just doing it an alternate way then the other one in the code to support more browsers.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users