html avoid dragging image code example

Example 1: how to disable image dragging in html

use ondragstart="return false" for dragging and onselectstart="return false" for selecting

Example 2: disable image dragging and right click

<!--Place the javascript code at the button of the page -->
     
     <script>
          $('img').bind('contextmenu', function(e) {
            return false;
        }); 
        
        $('#nearestStaticContainer').on('contextmenu', 'img', function(e){ 
          return false; 
        });
        
        $('body').on('contextmenu', 'img', function(e){     return false; });
        
        $(function(){
             $('body').on('contextmenu', 'img', function(e){ 
                 return false; 
             });
         });
        
         const images = document.getElementsByTagName('img');
        for (let i = 0; i < images.length; i++) {
            images[i].addEventListener('contextmenu', event => event.preventDefault());
        }
        
        $('img').bind('contextmenu', function(e){
            alert("Action is prohibited");return false;
        });
        </script>

Tags:

Html Example