
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Stop making form to reload a page in JavaScript
Let’s say what we need to achieve is when the user submits this HTML form, we handle the submit event on client side and prevent the browser to reload as soon as the form is submitted
HTML form
<form name="formcontact1" action="#"> <input type='text' name='email' size="36" placeholder="Your e-mail :)"/> <input type="submit" name="submit" value="SUBMIT" onclick="ValidateEmail(document.formcontact1.email)" /> </form>
Now, the easiest and the most reliable way of doing so is by tweaking our ValidateEmail() function to include the following line right at the top of its definition −
function ValidateEmail(event, inputText){ event.preventDefault(); //remaining function logic goes here }
What preventDefault() does is that it tells the browser to prevent its default behaviour and let us handle the form submitting event on the client side itself.
The full HTML code for this is −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <form name="formcontact1" action="#"> <input type='text' name='email' size="36" placeholder="Your e-mail :)"/> <input type="submit" name="submit" value="SUBMIT" onclick="ValidateEmail(document.formcontact1.email)" /> </form> <script> { function ValidateEmail(event, inputText){ event.preventDefault(); //remaining function logic goes here } } </script> </body> </html>
- Related Articles
- How to reload the current page without losing any form data with HTML?
- How to stop refreshing the page on submit in JavaScript?
- How to stop form submission using JavaScript?
- How to stop a page loading from Selenium in chrome?
- How to Stop the page loading in firefox programmatically in Selenium ?
- How can a page be forced to load another page in JavaScript?
- How to force Chrome's script debugger to reload JavaScript?
- How to stop forEach() method in JavaScript?
- How to reload activity in Android?
- How to stop a function during its execution in JavaScript?
- How to print a page using JavaScript?
- Hot Reload in ElectronJs
- How to Hot-Reload in ReactJS Docker?
- How to create a FAQ page using JavaScript
- How to stop a loop with a stop button in Tkinter?

Advertisements