Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8127665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:36:46+00:00 2026-06-06T07:36:46+00:00

I have a simple html form with few fields in it. I wanted to

  • 0

I have a simple html form with few fields in it.

I wanted to make sure that user gets an alert message when he is trying to leave the page. So I added the script on the page which will run whenever user is trying to navigate away from this page.

But I am also getting this alert message when user is trying to submit the form. I dont want this alert message when user is saving the form.

Please help.

Below is some sample code.

<html>
    <head>

    <script>
        function verifyexit() { return 'Are you sure ?'; }           window.onbeforeunload = verifyexit;
    </script>       
</head>
<body>
        Go to <a href="b.html"> another page </a> 
        <form action="c.html">
            name : <input type="text" name="name" value=""/> <br>
            email : <input type="text" name="email" value=""/> <br>
            <input type="submit" value="save"/>
        </form>
</body> 

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:36:47+00:00Added an answer on June 6, 2026 at 7:36 am

    stackoverflow reference

    To turn it on:

    window.onbeforeunload = "Are you sure you want to leave?";
    

    To turn it off:

    window.onbeforeunload = null;
    

    Bear in mind that this isn’t a normal event – you can’t bind to it in the standard way.

    To check for values? That depends on your validation framework.

    In jQuery this could be something like (very basic example):

    $('input').change(function() {
        if( $(this).val() != "" )
            window.onbeforeunload = "Are you sure you want to leave?";
    });
    

    With JQuery this stuff is pretty easy to do. Since you can bind to sets.

    Its NOT enough to do the onbeforeunload, you want to only trigger the navigate away if someone started editing stuff.

    To make this work in Chrome and Safari, you would have to do it like this

    window.onbeforeunload = function (e) {
        e = e || window.event;
    
        var msg = "Sure you want to leave?";
    
        // For IE and Firefox prior to version 4
        if (e) {
            e.returnValue = msg;
        }
    
        // For Safari
        return msg;
    };
    

    This is the general rule in reference

    window.onbeforeunload = function (e) {
      e = e || window.event;
    
      // For IE<8 and Firefox prior to version 4
      if (e) {
        e.returnValue = 'Any string';
      }
    
      // For Chrome, Safari, IE8+ and Opera 12+
      return 'Any string';
    };
    

    reference: https://developer.mozilla.org/en/DOM/window.onbeforeunload

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple html form that looks like the following <form action=search.php method=get>
I have a simple html form that is structured something like this: <form name=test
I have a few simple html text inputs. I would like to check that
I have a simple contact form on a website that has 2 text fields,
I have a website that has an index.php, a few simple .html pages and
I have a simple html form. This form has a specific width and margin
I have a simple HTML Form with one column having a select menu with
I have a simple html form. On php page. A simple list is placed
I have a simple HTML form. I'd like the right widgets in the second
I have a simple html login form which is validated with javascript before its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.