1

I just started learn about right click menu.
I would like to use it for some basic funtions like youtube search, image search etc.
But the problem is, when I override the default browser right click menu, I can say bye-bye for the copy and paste function, and that best I know, no way I can keep it.
I heard about some jquery javascript plugins, which can do copy and paste function, but the problem is not too safe, and not working always and the browser security policy also change always, so this is not the best way for I can keep this function.
Any idea how can I create my own javascript menu with the default copy and paste option?

  • 1
    I personally dislike when websites create custom right-click menus. It's never obvious that you can right-click on elements, plus I like to have my copy & paste working :-P – gen_Eric Dec 30 '14 at 21:19
  • If we are done, we create a website tour option with evbery feature, so if the user like it, use it. Otherwise, this is very good on a comment box, once you see it if you pase a link to the textarea, so, you can see the menu. But I dosen't like implement this, if I no find a way for I can keep copy and paste. Becouse this will be a shity design. – New Bie in Programing Dec 30 '14 at 21:23
  • The best way is to have an "icon" next to some elements (or sticky in one corner) and when it is left-clicked, the it shows a menu with extra action options. Thats better than right-click menu. IMO – Sam Battat Dec 30 '14 at 21:30
  • We have a button, but no one use it... Just copy and paste... I hate this, but somehow I need to notice the user, for he can embed a video esaly. If I create a very big button, also dosen't matter, becouse everybuddy just ignore it, becouse they belive ads. or something bad. – New Bie in Programing Dec 30 '14 at 21:34

1 Answers1

1

As you noted, most solutions use something like a "hidden" Flash object to handle clipboard interactions from JavaScript. This is because while you can create a ClipboardEvent in all browsers, triggering it on the document will either have no effect or result in an error. Affecting the clipboard is still a sandboxed behavior.

I would echo some of the above comments and suggest you do not replace the existing menu if possible. However in Firefox, and other browsers as they add support for it, there is a workaround where you can add menu items to the existing context menu:

<menu type="context" id="mymenu">
  <menuitem label="Do something" id="something"></menuitem>
</menu>
<div contextmenu="mymenu">
    Any context menu in this element will have the items added to it from mymenu.
</div>

And the event for the menu item can be handled like so:

document.getElementById("something").addEventListener("click", function () { ... });

The spec for this is possibly still being worked on, and Firefox's implementation doesn't exactly follow the spec. However for the time being it does work as a way of adding your own items and preserving the existing context menu.

Live example: http://jsfiddle.net/1ehtfjz6/5/

Art McBain
  • 400
  • 3
  • 8
  • can you crate a js fiddle or jsbin for I can see how it works? – New Bie in Programing Dec 30 '14 at 21:42
  • 1
    Updated the comment with one. – Art McBain Dec 30 '14 at 21:46
  • 1
    Hmm thank you! I realy sorry for only work on firefox. – New Bie in Programing Dec 30 '14 at 21:50
  • Well so am I. :) I wish more would implement it. – Art McBain Dec 30 '14 at 21:52
  • I hate flash solution becouse somany people use flash blocking for adds, and other reaseons (I also use it, becouse I hate porn ads on torrent sites etc). I hope, we can get a good api from google int the future for this. – New Bie in Programing Dec 30 '14 at 21:54
  • 1
    There is one, it's just sandboxed. That is normal web code isn't allowed to use it. It's all about security. Arbitrary web code reading your clipboard doesn't sound like a nice thing. In the end for usability reasons I think the approach above of adding things to the existing context menu is a better way to go. – Art McBain Dec 30 '14 at 21:59
  • This strange but not working with two option and I don' know why? http://jsbin.com/yoyajavili/1/edit – New Bie in Programing Dec 31 '14 at 14:22
  • 1
    Updated my answer. Despite what MDN shows in examples, the element appears to need a closing tag. In your example the browser automatically created one for you, and enclosed the second menuitem inside the first one causing it not to appear. As Stackoverflow is indicating we're getting a bit "chatty" down here, I would recommend starting a new top-level Question if you have further issues or hit up Stackoverflow's chat. – Art McBain Dec 31 '14 at 20:46
  • Sorry for my little stupid missunderstanding! I belived this is somthing simular tag like `` – New Bie in Programing Jan 02 '15 at 21:44
  • 1
    So did I. My original example didn't have a closing tag, because when I first found out about these, they didn't need one. – Art McBain Jan 03 '15 at 23:36