Displaying "Are you sure you want to leave" message when certain conditions are met

Is there a way to display the "Are you sure you want to leave?" message only if certain conditions are met?
I've tried some pretty basic onbeforeunload event triggers which work, but it literally fires on everything.
Mike MillerSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SriVaddadiCommented:
onbeforeunload is the right event. it would trigger every time before the page is unloaded.

You will have to write your logic there to check your conditions before prompting the user.
0
sammySeltzerCommented:
So many ways to handle this and SriVaddadi is correct, that's one of them

Something like:

<script type="text/javascript">
$(function () {
    $("input, textarea, select").on("input change", function() {
        window.onbeforeunload = window.onbeforeunload || function (e) {
            return "Are you sure you want to leave?";
        };
    });
    $("form").on("submit", function() {
        window.onbeforeunload = null;
    });
})
</script>

Then:

<input type='text' onchange='changes=true;'> </input>

Open in new window


Could be textarea or <SELECT>...</SELECT>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike MillerSoftware EngineerAuthor Commented:
That's pretty perfect Sammy. So on my submit button (where I don't want the popup, I could do like onclick='changes=false'?

What would I do to detect the change of a drop down?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

sammySeltzerCommented:
No, it is already there:

    $("form").on("submit", function() {
         window.onbeforeunload = null;
     });

Open in new window

0
Mike MillerSoftware EngineerAuthor Commented:
Ah so it is. Sorry about that! Works great.

What about the dropdown menus? Or like a checkbox, radio button, etc.? Would I need different functions for each of those?
0
sammySeltzerCommented:
I  believe you can add any control you want.

See where it says  $("input, textarea, select").on...

So, I suppose you can invoke the same onchange event on your <SELECT> which is your dropdown.
0
Mike MillerSoftware EngineerAuthor Commented:
You're right. I changed it up a little bit to account for some other controls. Still haven't figured out comboboxes just yet. Here's the updated code...

        <script type="text/javascript">
            $(function () {
                $("input, textarea, select, checkbox, radio, #dropdown-id").on("input change", function () {
                    window.onbeforeunload = window.onbeforeunload || function (e) {
                        return "Are you sure you want to leave?";
                    };
                });
                $("form").on("submit", function () {
                    window.onbeforeunload = null;
                });
            })
        </script>

Open in new window

0
sammySeltzerCommented:
Don't tell me this works #dropdown-id.

It shouldn't.

It should just be select as I had it.

That's because your textbox begins with <input

Your dropdown begins <select...
0
Mike MillerSoftware EngineerAuthor Commented:
None of my dropdowns have the word select in them. Here's an example...

<asp:DropDownList ID="ddlElementName" class="tbElementNM" name="tbElementNM" runat="server" Width="180" Height="21" Font-Names="Verdana" ForeColor="#666666" Style="font-size:12px;" onchange='changes=true;'></asp:DropDownList>

Open in new window

0
sammySeltzerCommented:
I understand.

That's because you are using asp.net.

Once the code is rendered, right-click on the app and click VIEW-SOURCE and you see <Select.

That's how asp.net works.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.