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

What is the Recommended Replacement for the Outdated event.returnValue in JavaScript?

DDD
Release: 2024-10-21 14:15:30
Original
977 people have browsed it

What is the Recommended Replacement for the Outdated event.returnValue in JavaScript?

Issue with event.returnValue Deprecated Recommendation in Chrome Console

When attempting to execute the JavaScript code below, you may encounter a warning in the Google Chrome console:

$(document).ready(function () {
    $("#changeResumeStatus").click(function () {
        $.get("{% url 'main:changeResumeStatus' %}", function (data) {
            if (data['message'] == 'hidden') {
                $("#resumeStatus").text("скрыто");
            } else {
                $("#resumeStatus").text("опубликовано");
            }
        }, "json");
    });
});
Copy after login

The warning reads, "event.returnValue is deprecated. Please use the standard event.preventDefault() instead."

Explanation

This warning arises because event.returnValue is an outdated property for preventing default browser actions. Its replacement, event.preventDefault(), adheres to modern web standards and is recommended for use.

jQuery Compatibility

In jQuery версии 1.10.2 (#changeResumeStatus being a span) still defaults to event.returnValue. However, jQuery 1.11 and later versions use event.preventDefault() by default.

Solution

To resolve the issue, you can manually add event.preventDefault() to the click event handler:

$("#changeResumeStatus").click(function (event) {
    event.preventDefault();
    $.get("{% url 'main:changeResumeStatus' %}", function (data) {
        if (data['message'] == 'hidden') {
            $("#resumeStatus").text("скрыто");
        } else {
            $("#resumeStatus").text("опубликовано");
        }
    }, "json");
});
Copy after login

The above is the detailed content of What is the Recommended Replacement for the Outdated event.returnValue in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

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