How to disable right click on IFRAME

前端 未结 6 2199
花落未央
花落未央 2020-12-11 18:14

I have a document content displayed on IFrame in MVC web application. The content should not be copied and printed . I tried to disable right click using two functions style

6条回答
  •  不思量自难忘°
    2020-12-11 18:24

    1.) Disabling a right-click in iFrame using jquery

    
    // Function to block the right click in the iFrame
    
    

    Call the "injectJS()" function in the iFrame

    
    

    2.) Disable the right-click in the webpage

    With javascript alone

    document.addEventListener('contextmenu', event => event.preventDefault());
    

    Here's an example in jQuery (Note: pressing the right mouse button will fire three events: the mousedown event, the contextmenu event, and the mouseup event)

    // With jQuery
    $(document).on({
    "contextmenu": function(e) {
        console.log("ctx menu button:", e.which); 
        // Stop the context menu
        e.preventDefault();
    },
    "mousedown": function(e) { 
        console.log("normal mouse down:", e.which); 
    },
    "mouseup": function(e) { 
        console.log("normal mouse up:", e.which); 
    } 
    });
    

    If you have any questions leave a comment below.

提交回复
热议问题