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.
I've tried some pretty basic onbeforeunload event triggers which work, but it literally fires on everything.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
What would I do to detect the change of a drop down?
No, it is already there:
$("form").on("submit", function() {
window.onbeforeunload = null;
});
ASKER
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?
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.
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.
ASKER
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>
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...
It shouldn't.
It should just be select as I had it.
That's because your textbox begins with <input
Your dropdown begins <select...
ASKER
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>
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.
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.
You will have to write your logic there to check your conditions before prompting the user.