javascript - Disable mobile longpress context menu on specific elements - Stack Overflow

I have an image gallery with various controls. One of the controls is a basic delete function, to delet

I have an image gallery with various controls. One of the controls is a basic delete function, to delete you click and hold for approx 1 second to get a confirmation asking if you want to delete. All works fine, it's just that on mobile devices it often causes the "Save Image As, etc" menu to pop up which has to be closed before the intended action can be performed.

I've read about various fixes but none of them seem to work with current versions of Chrome mobile on my Galaxy S5, and the most recent answer I could find was from 2013.

I found one saying that the context menu was it's own function, so I tried something like this:

    window.oncontextmenu = function(event) {
        event.preventDefault();
        event.stopPropagation();
        return false;
    };

But it did not prevent the context menu from showing on my S5. As I said, I'm hoping to find a solution to prevent it from ing up on certain items, not the entire window.

Thanks to Tasos for the answer

document.getElementById('yourElement').oncontextmenu = function(event) {
    event.preventDefault();
    event.stopPropagation(); // not necessary in my case, could leave in case stopImmediateProp isn't available? 
    event.stopImmediatePropagation();
    return false;
};

I have an image gallery with various controls. One of the controls is a basic delete function, to delete you click and hold for approx 1 second to get a confirmation asking if you want to delete. All works fine, it's just that on mobile devices it often causes the "Save Image As, etc" menu to pop up which has to be closed before the intended action can be performed.

I've read about various fixes but none of them seem to work with current versions of Chrome mobile on my Galaxy S5, and the most recent answer I could find was from 2013.

I found one saying that the context menu was it's own function, so I tried something like this:

    window.oncontextmenu = function(event) {
        event.preventDefault();
        event.stopPropagation();
        return false;
    };

But it did not prevent the context menu from showing on my S5. As I said, I'm hoping to find a solution to prevent it from ing up on certain items, not the entire window.

Thanks to Tasos for the answer

document.getElementById('yourElement').oncontextmenu = function(event) {
    event.preventDefault();
    event.stopPropagation(); // not necessary in my case, could leave in case stopImmediateProp isn't available? 
    event.stopImmediatePropagation();
    return false;
};
Share Improve this question edited Apr 16, 2016 at 22:12 SISYN asked Apr 16, 2016 at 18:38 SISYNSISYN 2,2695 gold badges26 silver badges46 bronze badges 3
  • Have you added this CSS -webkit-user-select: none; to disable text selection on long press. I think you are not facing this problem – rajesh Commented Apr 16, 2016 at 18:52
  • 1 theres also (event.stopImmediatePropagation();) -- try putting that underneath (event.stopPropagation();) -- otherwise check here stackoverflow./questions/12304012/… – Tasos Commented Apr 16, 2016 at 18:53
  • 2 The CSS property does not work either. Tasos, please post an answer with your stopImmediateProp solution because it worked and I would like to give you credit. Will be updating the original question as well. Thanks! – SISYN Commented Apr 16, 2016 at 22:10
Add a ment  | 

1 Answer 1

Reset to default 6

I (re)post the answer here because at first, I haven't seen it was in the question :)

So juste use this code, with stopImmediatePropagation :

document.getElementById('yourElement').oncontextmenu = function(event) {
    event.preventDefault();
    event.stopPropagation(); // not necessary in my case, could leave in case stopImmediateProp isn't available? 
    event.stopImmediatePropagation();
    return false;
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744136874a4560098.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信