17
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

よくWebOSやJSで禁止されるキー設定

Last updated at Posted at 2014-11-27

F5,Ctrl + F5, Ctrl + R を禁止

var event = e || window.event;
var k = event.keyCode;
if((event.ctrlKey == true && k == 82) || (event.ctrlKey == true && k == 78) || (k == 116) || (event.ctrlKey == true && k == 116))
{
    event.keyCode = 0;
    event.returnValue = false;
    event.cancelBubble = true;
    return false;
}

右クリックのメニューを禁止

document.oncontextmenu = function(){
    return false;
};

ドロップを禁止

document.ondragstart = function(){
    return false;
};

セレクトを禁止、inputとtextareaを除外

document.onselectstart = function( e ){
    var event = e || window.event;
    var tagName = '';
    try{
        tagName = (event.target || event.srcElement).tagName.toLowerCase();
    }
    catch(e){}
    if( tagName != 'textarea' && tagName != 'input'){
        return false;
    }
}

リンクを新ウインドウで開くことを禁止

document.onclick = function( e ){
    //Shift + click 及び Ctrl + click を禁止

    var event = e || window.event;

    var tagName = '';
    try{
        tagName = (event.target || event.srcElement).tagName.toLowerCase();
    }
    catch(e){}

    if( (event.shiftKey || event.ctrlKey) && tagName == 'a' ){
        event.keyCode = 0;
        event.returnValue = false;
        event.cancelBubble = true;
        return false;
    }
}

以上!

17
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?