What are the steps to enable JavaScript before the next webpage finishes loading?

I am looking to create a loading screen that triggers when a Django form is submitted. Here is an example code snippet:

forms.py

class ExampleForm(forms.Form):
    example_field = forms.CharField(
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )

index.html

<style>
    .overlay {
        display: none;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(255,255,255,0.8);
        z-index: 5000;
        text-align: center;
        padding-top: 20%;
    }
</style>
<form action="example_form">
    {% for each in example_form %}
        {{ each.label_tag }}
        {{ each  }}
    {% endif %}
    <button type="submit" class="btn btn-primary" name="action" value="submit">submit</button>
</form>
<script>
    $(document).ready(function () {
        function ShowLoading() {
            $(".overlay").show();
        }

        $("button[type='submit']").on("click", ShowLoading)
      })
</script>

This implementation displays the loading screen upon clicking the submit button before the next page loads. However, an issue arises when attempting to submit the form without filling out the mandatory field. Despite an input tooltip reminder appearing, the loading screen still shows up because of the click trigger regardless if a new page is being loaded or not.

I am seeking a solution where the loading screen activates as the next page loads but prior to its actual appearance. Is there a way for JavaScript to detect this transition?

Answer №1

Exchange

$("button[type='submit']").on("click", DisplayLoading)

substitute for

$("form").on("submit",DisplayLoading);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Focus on the iPad3 model specifically, excluding the iPad4

I am looking to apply specific CSS that works on iPad 3, but not on iPad 4, or vice versa. Currently, I am able to target both versions by using the following code: @media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and ( m ...

Anticipating the refresh of React state in a functional component

I am looking for a way to ensure that the function handleOpen is only called after all the state variables (nameError, emailError, messageError) have been updated. The issue I am facing is that the state update is not instantaneous, causing handleOpen to s ...

Instructions on creating a countdown timer that reveals a hidden div once it reaches zero, remains visible for a set period, and then resets

I created a countdown timer that displays a div when the timer reaches zero. However, I am struggling with getting the timer to reset and start counting down again after displaying the div. For instance, if I set the timer for 7 days and it reaches the de ...

Can the same form be submitted with two different actions?

I am facing an issue with a form that is supposed to submit data to 2 different pages using the POST method. After trying some javascript code, I found that one form submission works correctly while the other does not. <form id="add"> <input ...

Performing calculations within handsontable

Trying to figure out how to concatenate values from a handsontable grid, I stumbled upon some code on jsfiddle that caught my eye. Here is the link: http://jsfiddle.net/9onuhpn7/4/ The task at hand involves 3 columns A,B,C and an attempt to concatenate ...

Struggling with ajax: Converting a JavaScript variable to a PHP variable

I'm trying to convert a JavaScript variable into a PHP variable in order to use it in an SQL query, but for some reason it's not working as expected. Here is the HTML code: <select id = "dep_ID" name = "dep_ID" onchange="myFunction()"> A ...

Converting array elements to strings when extracting in JSON with JavaScript

I am working with a Json document that looks like this: { "_id": "13fee4aad95c7f822c0b559bd8d09fb0", "_rev": "5-336dea3680af3e7ec0de29369be90b09", "attributeCollection": { "attributeArray": [ { "name": "Web Issue", "val ...

Disabling Immediate Row Checkbox in Angular Datatable Based on Column Data

Is there a way to prevent the checkbox in a row from being checked based on certain column data? In my datatable, I need to implement a condition where if firstname="Superman", then the checkbox for that particular row should be disabled to preve ...

Managing Flicker Effect by Implementing Theme Switching and Using Local Storage in Next.js with Ant Design

I've been working on a new feature to switch themes (light/dark) dynamically in a Next.js application using Ant Design. Successfully integrating the theme switch with a toggle switch and useState hook, I'm faced with the challenge of storing the ...

Is there a method for redirecting my page to a specific href link without triggering a page reload?

This is my HTML code. <a href="http://127.1.1.0:8001/gembead/emstones.html?car=36">Car</a> I am trying to redirect to a specific page with parameters without fully reloading the current page. Is there a way to achieve this task? I believe the ...

How can I submit multiple dropdown menus when they are changed?

I recently added Four dropdown menus on my index.php page. <select name="filter_month" class="filters"> <option>Select a Month</option> <option value="1">January</option> <option value="2">February</optio ...

Exploring the Implementation of Box Icons in a Vuejs For Loop

I am currently exploring the box icons web component and I am trying to integrate the icons into my link array for each item. However, I am uncertain about the best approach to achieve this. <div class="container"> <div class="l ...

"Exploring the process of looping through an array of JavaScript objects and showcasing a List Selector on the Google Assistant app with the help of

After extensively researching all the available documentation at https://developers.google.com/actions/assistant/responses In the documentation for the "List Selector," all the examples provided involve static and predetermined data. In a real-world scen ...

What is preventing the click event on this dynamically generated checkbox using jQuery?

Check out the jsFiddle Currently, I am utilizing a jQuery plugin that enables users to draw boxes within a designated area. Using jQuery, I have incorporated a checkbox (alongside a dropdown list) into the box that appears when the user releases the mouse ...

Exploring the potential of using nested @transaction.commit_on_success in Django

Imagine this straightforward example: # representing a bank account class Account: @transaction.commit_on_success def withdraw(self, amount): # code for money withdrawal @transaction.commit_on_success def add(self, amount): ...

What changes can be made to this function in order for the arches to be positioned directly in front of the camera?

I devised a method that arranges arches in front of a camera, side by side: <!-- HTML --> <a-curvedimage v-for="(value, index) in model" :theta-length="42" :rotation="setThumbsRotation(value, index)"> </a-curvedimage> <a-camera ...

Run the JavaScript configuration (Object) prior to converting it to JSON format

Is there a way to implement functions within a JavaScript file (configuration for backend server) that execute before parsing the data to JSON? For example: config.js module.exports = { structure_layout: { BUILDING: "BUILDING", FLOOR: ...

The function getComputedStyle(elem).getPropertyValue('fontSize') returns incorrect values when the font size of the element is specified in em units

My current issue involves trying to obtain the font size of an element in the following manner: getComputedStyle(MyTargetElement , "").getPropertyValue('font-size') The result is coming back as 16px, when it should actually be 14px. W ...

Image not showing up on HTML page following navigation integration

Having trouble with my responsive website setup - the background image for ".hero-image" isn't showing up after adding the navigation. Google Chrome console is throwing a "Failed to load resource: the server responded with a status of 404 (Not Found)" ...

Exploring the DOM with jQuery to effectively select multiple elements

I am currently attempting to locate and select all elements with the "findme" class, and then identify the selects that are located nearby. http://jsfiddle.net/R93md/1/ Specifically, I have experimented with $(".findme").parent().prev().first(); Once ...