Link to home
Start Free TrialLog in
Avatar of Member_2_1242703
Member_2_1242703

asked on

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.
Avatar of Ravi Vaddadi
Ravi Vaddadi
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of sammySeltzer
sammySeltzer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_1242703
Member_2_1242703

ASKER

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?
No, it is already there:

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

Open in new window

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?
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.
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

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...
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

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.