Javascript - Prevent right-click on scrollbar to see page source code

Purpose is to prevent user from seeing web page source code by clicking right-click view page source. Also not to take any content copy paste.

$(document).ready(function () {

  $('body')
    .attr('unselectable', 'on')
    .css('user-select', 'none')
    .on('selectstart dragstart', false);

//Disable cut copy paste
$('body').bind('cut copy paste', function (e) {
    e.preventDefault();
});

//Disable mouse right click
$("body").on("contextmenu",function(e){
    return false;
});
});

I used this code and it works nice except for 2 cases:

1- The scrollbar. user can still right-click on it and choose view page source.

2- CTRL + U (chrome short cut to do the same job)

any suggestions?

EDIT: CTRL + U Disabled by

document.onkeydown = function(e){
        if (e.ctrlKey &&
            (e.keyCode === 67 ||
                e.keyCode === 86 ||
                e.keyCode === 85 ||
                e.keyCode === 117)) {
            return false;
        } else {
            return true;
        }
    };

Answers 1

  • use something like this

    document.onkeydown = function(e) {
        if (e.ctrlKey && 
            (e.keyCode === 67 || 
             e.keyCode === 86 || 
             e.keyCode === 85 || 
             e.keyCode === 117)) {
            console.log('not allowed');
            return false;
        } else {
            return true;
        }
    

    };

    to restrict ctrl+U.

    Keycodes according to mozilla

    CANCEL=     3
    HELP=   6
    BACK_SPACE=     8
    TAB=    9
    CLEAR=  12
    RETURN=     13
    ENTER=  14
    SHIFT=  16
    CONTROL=    17
    ALT=    18
    PAUSE=  19
    CAPS_LOCK=  20
    KANA=   21
    HANGUL=     21
    JUNJA=  23
    FINAL=  24
    HANJA=  25
    KANJI=  25
    ESCAPE=     27
    CONVERT=    28
    NONCONVERT=     29
    ACCEPT=     30
    MODECHANGE=     31
    SPACE=  32
    PAGE_UP=    33
    PAGE_DOWN=  34
    END=    35
    HOME=   36
    LEFT=   37
    UP=     38
    RIGHT=  39
    DOWN=   40
    SELECT=     41
    PRINT=  42
    EXECUTE=    43
    PRINTSCREEN=    44
    INSERT=     45
    DELETE=     46
    0=  48
    1=  49
    2=  50
    3=  51
    4=  52
    5=  53
    6=  54
    7=  55
    8=  56
    9=  57
    SEMICOLON=  59
    EQUALS=     61
    A=  65
    B=  66
    C=  67
    D=  68
    E=  69
    F=  70
    G=  71
    H=  72
    I=  73
    J=  74
    K=  75
    L=  76
    M=  77
    N=  78
    O=  79
    P=  80
    Q=  81
    R=  82
    S=  83
    T=  84
    U=  85
    V=  86
    W=  87
    X=  88
    Y=  89
    Z=  90
    CONTEXT_MENU=   93
    SLEEP=  95
    NUMPAD0=    96
    NUMPAD1=    97
    NUMPAD2=    98
    NUMPAD3=    99
    NUMPAD4=    100
    NUMPAD5=    101
    NUMPAD6=    102
    NUMPAD7=    103
    NUMPAD8=    104
    NUMPAD9=    105
    MULTIPLY=   106
    ADD=    107
    SEPARATOR=  108
    SUBTRACT=   109
    DECIMAL=    110
    DIVIDE=     111
    F1=     112
    F2=     113
    F3=     114
    F4=     115
    F5=     116
    F6=     117
    F7=     118
    F8=     119
    F9=     120
    F10=    121
    F11=    122
    F12=    123
    F13=    124
    F14=    125
    F15=    126
    F16=    127
    F17=    128
    F18=    129
    F19=    130
    F20=    131
    F21=    132
    F22=    133
    F23=    134
    F24=    135
    NUM_LOCK=   144
    SCROLL_LOCK=    145
    COMMA=  188
    PERIOD=     190
    SLASH=  191
    BACK_QUOTE=     192
    OPEN_BRACKET=   219
    BACK_SLASH=     220
    CLOSE_BRACKET=  221
    QUOTE=  222
    META=   224
    

    Restrict Mouse right click on scrollbar try this following

    var clickedOnScrollbar = function(mouseX)
    {
       if( $(window).outerWidth() <= mouseX ){
            return true;   
        } 
        }
    
        $(document).mousedown(function(e){ 
        var isRightMB;
            e = e || window.event;
    
            if ("which" in e)  
                isRightMB = e.which == 3; 
            else if ("button" in e)  
                isRightMB = e.button == 2; 
    
    
            if(isRightMB){  
         if( clickedOnScrollbar(e.clientX) ){
            alert("Right click is not allowed"); 
          }
    } 
        });
    

Related Articles