JavaScript FAQ (15)--mouse event (ii)

Source: Internet
Author: User

12. Mouse Events

2. Left vs. right button

Q: How do I check whether the user clicks on the right or left button.

A: The Click event only occurs on the left key, so the OnClick event handler does not have to be tested on the left and right.

On the other hand, the MouseDown and MouseUp events may occur on any of the mouse keys. To determine whether the user clicks the left or right button, you can use the following event properties: Netscape Navigator Event.which Internet Explorer Event.button

If the value of these properties is 1, the event occurs on the left key. In the following example, the Onmouseevent event handler displays the left button or right button based on the mouse you actually clicked. This message will appear on the status bar of your browser. Click or right-click anywhere on the page to see how it works:

<script language= "JavaScript" ><!--
function MouseDown (e) {
 if (parseint (navigator.appversion) > 3) {
  var clicktype=1;
  if (navigator.appname== "Netscape") Clicktype=e.which;
  else Clicktype=event.button;
  if (clicktype==1) self.status= ' left button! ';
  if (clicktype!=1) self.status= ' right button! ';
 }
 return true;
}
if (parseint (navigator.appversion) >3) {
 document.onmousedown = MouseDown;
 if (navigator.appname== "Netscape") 
  document.captureevents (Event.mousedown);
}
--></script>

3. Right key: Not available (right-hand button:disabling)

Q: I can use the OH transfer right click does not eject the Windows context menu.

a: In most browsers today, you can block pop-up context menus by using the OnContextMenu time processor in the Body tab of the page.

<body oncontextmenu= "return false;" >

Try right-clicking anywhere on the page--the context menu doesn't appear.

In older browsers (from Netscape Navigator 4.x and Internet Explorer 4.x You can disable the right-click menu by displaying a warning message when you right-click.) Insert the following code into the <HEAD> area of the page to complete the above function:

<script language= "JavaScript" ><!--
function MouseDown (e) {
 if (parseint (navigator.appversion) > 3) {
  var clicktype=1;
  if (navigator.appname== "Netscape") Clicktype=e.which;
  else Clicktype=event.button;
  if (clicktype!=1) {
   alert (' right mouse button is disabled. ')
   return false;
  }
 }
 return true;
}
if (parseint (navigator.appversion) >3) {
 document.onmousedown = MouseDown;
 if (navigator.appname== "Netscape") 
  document.captureevents (Event.mousedown);
}
--></script>

Note: context menus are still present in particularly old browsers (Netscape Navigator 3.x, Internet Explorer 3.x, or earlier). Also, if the user has disabled JavaScript, the context menu will appear.

4. No right-click menu on picture (Disabling the right-click menu for an image)

Q: I can only disable the right-click menu on one picture, while other pictures still show.

A: That 's OK. In most of today's browsing, you can disable the right-click menu on a particular picture. To implement this feature, you can use the event handler oncontextmenu= "return FALSE" in the img tag, picture definition:


For example, the right button menu for this picture has been banned:

Note: This technique does not work in older browsers (for example, Internet Explorer 4.x or earlier). If the user disables JavaScript, the menu will also appear.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.