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 8395793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:23:23+00:00 2026-06-09T20:23:23+00:00

I have been battling for the past two days with the evil onbeforeunload function

  • 0

I have been battling for the past two days with the evil onbeforeunload function in JavaScript. I have a function that warns the user when they are about to close a page.
However before the page close I would like to submit the form using JavaScript’s .submit().

This is my code:

function setPopUpWindow(submitForm){
    window.onbeforeunload = function () {
        if (submitForm == false ) {
        //alert("It worked");   --This code gets called so I know it works  
        document.getElementById("CancelScripting").submit();
        //return "Unsaved Data would be lost";
        }
        
    }
}

In my html I have two buttons, one is (supposed to) trigger the .submit() and the other will just ignore it.

<body>
 <form action=tett.html id="popUpForm" method=POST>
<script>setPopUpWindow();</script>
<input type="submit" id="submit_button" onclick="setPopUpWindow(true);">
<input class=b1 type=submit id="CancelScripting"  style="visibility:hidden"  value="CancelScripting" >
</body>

The `setPopWindow value for the second input is not defined so it would be false.
For some reason the submit is not working well.

————————Edit to my question———————————————–
I would like to submit the form even if the user leaves the page by closing the X button on their window. This is the reason why I have the hidden button… Looks like people misunderstood my question.

  • 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-09T20:23:24+00:00Added an answer on June 9, 2026 at 8:23 pm

    The only thing you can do is to ask the user if they really want to leave the page:

    <head>
        <script type="text/javascript">
            var submitForm = false;
            window.onbeforeunload = function () {
                if(submitForm == false){
                    return 'You have an unfinished form ...';
                }
            }
            function setPopUpWindow(type){
                submitForm = true;
            }
        </script>
    </head>
    <body>
       <form action="" method="post" name="SubmitForm" id="SubmitForm">
          <input type="submit" id="submit_button" onclick="setPopUpWindow(true);">
       </form>
    </body>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been battling like crazy with a javascript that allows for infinite scrolling
I have been battling a crash that occurs in my game engine for about
I've been battling with this issue for a couple of days now. I have
So, I've been battling with Javascript for a little while now and I have
I have been battling a problem now for about a week and it's really
I have been battling this error with specjour for the past couple hours, and
I have been battling this issue for a few days now and think I
I have been battling with this for a couple of days. I am trying
I have been battling with this piece of code for literally days now... Would
maybe someone could shed some light on the statement that I have been battling

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.