最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to generate a right-click event in all browsers - Stack Overflow

programmeradmin1浏览0评论

A little context:
The app I'm working on has a right-click context menu for certain objects on the screen. The current design as that each of these objects listens for a right-click, sends an AJAX request to get the context data for that object, uses that data to create a PopupMenu2 from Dojo 0.4.3 (I know!), and then generates a right-click to launch the Dojo menu.

I'm trying to figure out a way to generate a right-click event for all browsers. Currently, we only support IE and use the oncontextmenu event.

Restrictions:

  • No jQuery :(
  • I cannot pre-load all the data for the objects on screen to create the Dojo menu and avoid the AJAX request.

A little context:
The app I'm working on has a right-click context menu for certain objects on the screen. The current design as that each of these objects listens for a right-click, sends an AJAX request to get the context data for that object, uses that data to create a PopupMenu2 from Dojo 0.4.3 (I know!), and then generates a right-click to launch the Dojo menu.

I'm trying to figure out a way to generate a right-click event for all browsers. Currently, we only support IE and use the oncontextmenu event.

Restrictions:

  • No jQuery :(
  • I cannot pre-load all the data for the objects on screen to create the Dojo menu and avoid the AJAX request.
Share Improve this question edited Aug 18, 2019 at 10:09 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 16, 2009 at 18:18 ntownsendntownsend 7,64010 gold badges41 silver badges35 bronze badges 4
  • 2 harelmalka./rightcontext – powtac Commented Oct 16, 2009 at 18:21
  • 1 +1 for starting a question about right-click events with "A little context". – Troubadour Commented Oct 16, 2009 at 18:27
  • I'm glad someone caught that! – ntownsend Commented Oct 16, 2009 at 18:28
  • @powtac Thanks for the link but this doesn't really answer my question. I'm looking for a way to programatically generate right-click events, not capture "real" right-click events and do things with them. – ntownsend Commented Oct 16, 2009 at 18:34
Add a ment  | 

1 Answer 1

Reset to default 4

This should get you started with generating a right click event. The key to the right click is the button parameter: button = 2.

if (document.createEvent) {
  var rightClick = document.createEvent('MouseEvents');
  rightClick.initMouseEvent(
    'click', // type
    true,    // canBubble
    true,    // cancelable
    window,  // view - set to the window object
    1,       // detail - # of mouse clicks
    10,       // screenX - the page X coordinate
    10,       // screenY - the page Y coordinate
    10,       // clientX - the window X coordinate
    10,       // clientY - the window Y coordinate
    false,   // ctrlKey
    false,   // altKey
    false,   // shiftKey
    false,   // metaKey
    2,       // button - 1 = left, 2 = right
    null     // relatedTarget
  );
  document.dispatchEvent(rightClick);
} else if (document.createEventObject) { // for IE
  var rightClick = document.createEventObject();
  rightClick.type = 'click';
  rightClick.cancelBubble = true;
  rightClick.detail = 1;
  rightClick.screenX = 10;
  rightClick.screenY = 10;
  rightClick.clientX = 10;
  rightClick.clientY = 10;
  rightClick.ctrlKey = false;
  rightClick.altKey = false;
  rightClick.shiftKey = false;
  rightClick.metaKey = false;
  rightClick.button = 2;
  document.fireEvent('onclick', rightClick);
}

I would suggest Googleing for 'document.createEvent' and 'document.createEventObject' for more detail on the API from the Mozilla and MSDN sites.

Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论