URL being updated on preventdefault
Have a problem that is driving me insane! I suspect I am doing something wrong...
I am using jQuery mobile 1.4.5 with multiple pages in the same HTML file. jQuery version: 1.11.3
I have a page
#mainregister on which I check for any user changes. If they try to navigate away, I stop the navigation using this code (using info I found on SO):
- $(document).on("pageinit", '#mainregister', function(){
- $(document).on('pagebeforechange', function(e, data){
- ResetTimeOutTimer(); // Reset inactivity timer
- if (typeof data.toPage != "string") {
- if (isDirty) {
-
- var to = data.toPage.attr('id');
- to = '#' + to;
- console.log(to);
- if(to !== '#mainregister')
- {
- var confirmationMessage = 'It looks like you have been editing something. '
- + 'If you leave before saving, your changes will be lost.';
-
- e.preventDefault();
- e.stopPropagation();
- isDirty = false;
- console.log(to);
- $("#leavebutton").on('click', function(){
- changePage(to, false);
- return false; // This is to prevent events bubbling up and causing double page flips
- // See https://github.com/jquery/jquery-mobile/issues/2369#issuecomment-4830800
- // and https://css-tricks.com/return-false-and-prevent-default/
- });
- $("#leavetext").html(confirmationMessage);
- $('#confirmLeave').popup('open');
- return false;
- }
- }
- }
- });
-
- });
This brings up a popup asking the user if they wish to Save or Leave. This popup is defined as:
- <!-- Leave Warning Popup -->
- <div data-role="popup" id="confirmLeave" data-overlay-theme="a" data-theme="a" style="max-width:400px;" class="ui-corner-all">
- <div data-role="header" data-theme="a" class="ui-corner-top">
- <h1>Info</h1>
- </div>
- <div data-role="content" data-theme="d" class="ui-corner-bottom ui-content" id="leaveContent">
- <h3 class="ui-title" id="leavetext"></h3>
- <input type="button" value="Save" onclick="SaveRegister()"/>
- <input type="button" value="Leave" id="leavebutton"/>
- </div>
- </div>
- <!-- End Leave Warning Popup -->
Now, if the user goes 'back' from
#mainregister the navigation is stopped and the popup is shown. If the user chooses save, the save occurs but the URL still changes even though we're still on
#mainregister page.
So, user is on
index.html/#mainregister , hits back button, popup comes up, user chooses to save, popup closes, page shown is still
#mainregister but URL is now
index.html but should be
index.html/#mainregister .
So, now the URL and the actual page do not match; and navigation is a little broken from here.
Am I using the wrong page event? Or something else?
Also (perhaps related) I notice that if I go back after a popup (in other use cases) I get the same URL twice - e.g.
<path>\index.html\#<path>\index.html (is this because I am testing from file:// or something else)?