Home >Web Front-end >JS Tutorial >How to prevent page refresh with JavaScript

How to prevent page refresh with JavaScript

coldplay.xixi
coldplay.xixiOriginal
2020-11-10 15:16:408514browse

How to disable page refresh in JavaScript: 1. Disable F5 refresh, the code is [if (event.keyCode == 116)]; 2. Disable the right-click pop-up menu, the code is [document.oncontextmenu = function () 】.

How to prevent page refresh with JavaScript

The operating environment of this tutorial: windows10 system, javascript1.8.5, this article is applicable to all brands of computers.

How to disable page refresh in JavaScript:

The JavaScript code to disable page refresh is as follows:

1. Disable F5 refresh

document.onkeydown = function ()
{
    if (event.keyCode == 116) {
        event.keyCode = 0;
        event.cancelBubble = true;
        return false;
    }
}

2. Disable right-click pop-up menu

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

Related free learning recommendations: JavaScript (video)

The above is the detailed content of How to prevent page refresh with JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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 admin@php.cn