Is there a way to detect and stop if page is reloading.
I have a page which is getting reloaded after successful submission of a form present in it. I want to have an event listener which sees if page is reloading and should stop it from being reloaded.
I cannot return false;
for sucessful submit of registration form
In your html:
<form onsubmit="myFunction()"> Enter name: <input type="text"> <input type="submit"> </form>
In your javascript:
myFunction = function(e) { // prevents default action to happen e.preventDefault(); // do what ever you want to do here // ie perform a AJAX call }
You would have to submit the form using AJAX to avoid refreshing the entire page.
This can be accomplished with regular Javascript but is made easier with jQuery http://api.jquery.com/jquery.ajax/
Detecting if the page is reloading is a very dirty solution.
You have to prevent the default action
<script>
$( "a" ).click(function( event ) {
event.preventDefault();
});
</script>
In that case you'll need to implement the form sending mechanics via some kind of AJAX call.
Because sending a form is actually loading a new page with some parameters (which are the form data).
Here's how a standard form submission works:
Browser -> issues GET or POST HTTP request -> Server
Browser <- returns the result of the request <- Server
Browser re-renders the current page based on the server's response.
Therefore, a standard form submit will always incur a page reload.
However, you can use AJAX to make your HTTP request, which allows you to avoid a page reload.
Have a look at $.ajax
The default action after submit is to send to the page where it manipulates the data. if you are trying to stop the refresh, ie you are not submitting the form.
e.preventDefault()
. this will stop the default submit behavior, which includes reload of page[ but form is not submitted]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.