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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:44:53+00:00 2026-06-17T15:44:53+00:00

I currently have a webpage in a format similar to this, with a sidebar

  • 0

I currently have a webpage in a format similar to this, with a sidebar and iframes with sources pointing to other pages on my website.

enter image description here

I have the following code to offer user a confirm box for if they wish to leave the page:

window.onbeforeunload = function(e) {
    return 'All unsaved data will be lost.';
};

In the iframes, I have various HTML elements, such as textfields, input boxes, buttons, etc. I don’t want the user to get the onbeforeunload function unless they have clicked on one of these input elements first.

Of course, I could go in and code each textfield, button, etc, manually using by creating a variable such as shouldPopup, then using onclick to set shouldPopup to TRUE, and finally using an onbeforeunload if statement, but this is inconvenient and, if I were to perhaps make more changes in the future, I would potentially have to go in and modify each element one by one again.

What is a better way to get the same results? Any input as to how to simplify the code is greatly appreciated! 🙂

Update:

Some more information about my Iframes. I am using the jQuery library for some basic functions (nothing fancy). There’s quite a bit of PHP in them, as well as a few database requests, but nothing out of the ordinary. Hopefully this information is helpful to anyone looking to make any suggestions

  • 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-17T15:44:53+00:00Added an answer on June 17, 2026 at 3:44 pm

    If you don’t want to change too much of your code, I think toggeling a flag is OK. The following is a simple, pragmatic approach.

    Example form:

    <input type="text" class="form" ...>
    <input type="text" class="form" ...>
    <select class="form" ...>
       <option value=""></option>
       ...
    </select>
    

    Monitor changes in both iFrames:

    (function(){
      // This can be optimized to check against saved values on blur
      $("body").on("change", ".form", function(evt){
        window.top.$(window.top).trigger("change-form"); // trigger custom event in parent page
      });
    }());
    

    Parent page:

    (function(){
      var dirty = false
      $(window).on("change-form", function(){
        dirty = true;
      });
    
      window.onbeforeunload = function(e) {
        // Notify user only if forms are dirty
        return dirty ? 'All unsaved data will be lost.' : undefined;
      };
    }());
    

    This code is braindump, it’s untested.

    Note: If your pages are served from different domains than the parent page you’ll run into cross site scripting issues. In that case you’re not able to access the parent page’s Javascript. Then you have to use postMessage() to send messages to your parent page’s domain.


    I’d suggest you to take a look at Backbone.js:

    • https://github.com/documentcloud/backbone/wiki/Tutorials%2C-blog-posts-and-example-sites
    • http://backbonejs.org/#examples

    Edit 1:

    Backbone can help you managing the state of your form. In Backbone a Model will represent your form data. A View – representing your form (form’s inputs) – is bound to that Model. It will monitor your form fields and set() attributes on the model. A Backbone Model has its own state, you can check if the form has chnaged using e.g. the changed() method. Separating Model and View in your application is always a good idea, it helps separating the responsibilities.

    I just wanted to give you another idea.

    Edit 2:

    Fixed the code that triggers an event in the top window. I was not sure if $(window.top) works. It does not work. I’m not sure why. I’ve tested it and this code is correct:

    window.top.$(window.top). ...
    

    I’ve updated the above code.

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

Sidebar

Related Questions

I currently have this on a webpage I'm making: HTML <div id=pageHeader> <nav id=siteNav>
I currently have Google Maps integrated into my webpage. It is being used to
in my webpage I have a total in currency format that can either be
I am developing a webpage at http://www.knowledgenation.us and currently I have roughly 500 posts
Currently i have this procedure under package that gets user_name when some selects user_id
Suppose you have a webpage that is displaying data in columns. I'm currently separating
I currently have a webpage where an iframe contains data that is stored into
I currently have an app which reads a line from a webpage which looks
I currently have a small slideshow on my webpage that is being powered by
I have a string in my web page with the current format: This are

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.