Home > Web Front-end > JS Tutorial > body text

How to achieve the disabling refresh effect in javascript

醉折花枝作酒筹
Release: 2023-01-05 16:08:19
Original
5792 people have browsed it

JS method to disable refresh: first use "window.event" to detect the internal code corresponding to the keyboard event; then compare the internal code with the key value code of the "ctrl R", F5 key, and "ctrl F5" key Compare; if the values ​​are the same, set the "returnValue" value to false and cancel the refresh event.

How to achieve the disabling refresh effect in javascript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

document.onkeydown = function(e) {
        e = window.event || e;
        var k = e.keyCode;
        //屏蔽ctrl+R,F5键,ctrl+F5键  F3键!验证
        if ((e.ctrlKey == true && k == 82) || (k == 116)
                || (e.ctrlKey == true && k == 116)||k==114) {
            e.keyCode = 0;
            alert("当前页面不能刷新!");
            e.returnValue = false;
            e.cancelBubble = true;
            return false;
        }
        if (k == 8) {
            alert("不能返回或后退!");
            e.keyCode = 0;
            e.returnValue = false;
            return false;
        }
        //屏蔽 Ctrl+n   验证可以实现效果
        if (e.ctrlKey && k == 78){
            e.keyCode = 0;
            e.returnValue = false;
            e.cancelBubble = true;
            return false;
            }
        //屏蔽F11   验证可以实现效果
        if (k == 122) {
            e.keyCode = 0;
            e.returnValue = false;
            e.cancelBubble = true;
            return false;
        }
        //屏蔽 shift+F10  验证可以实现效果      
        if ((e.shiftKey && k == 121)||(e.ctrlKey && k == 121)){
            e.keyCode = 0;
            e.returnValue = false;
            e.cancelBubble = true;
            return false;
            }
        //屏蔽Alt+F4  
        if ((e.altKey) && (k== 115)) {
            window.showModelessDialog("about:blank", "",
                    "dialogWidth:1px;dialogheight:1px");
            e.keyCode = 0;
            e.returnValue = false;
            e.cancelBubble = true;
            return false;
        }
        //屏蔽 Alt+ 方向键 ← ;屏蔽 Alt+ 方向键 → !验证
        if ((e.altKey)
                && ((k == 37) || (k == 39))) {
            alert("不准你使用ALT+方向键前进或后退网页!");
            e.keyCode = 0;
            e.returnValue = false;
            e.cancelBubble = true;
            return false;
        }
    };
    
    //屏蔽右键菜单,!验证
    document.oncontextmenu = function(event) {
        if (window.event) {
            event = window.event;
        }
        try {
            var the = event.srcElement;
            if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) {
                return false;
            }
            return true;
        } catch (e) {
            return false;
        }
    };
Copy after login

【Recommended learning: javascript advanced tutorial

The above is the detailed content of How to achieve the disabling refresh effect in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!