{"id":33,"date":"2015-07-25T14:17:00","date_gmt":"2015-07-25T14:17:00","guid":{"rendered":"https:\/\/ahm.basfinans.com\/index.php\/2015\/07\/25\/managing-browser-back-forward-when-you-use-jquerymobile\/"},"modified":"2015-07-25T14:17:00","modified_gmt":"2015-07-25T14:17:00","slug":"managing-browser-back-forward-when-you-use-jquerymobile","status":"publish","type":"post","link":"https:\/\/ahm.basfinans.com\/index.php\/2015\/07\/25\/managing-browser-back-forward-when-you-use-jquerymobile\/","title":{"rendered":"Managing Browser Back\/Forward when you use JqueryMobile"},"content":{"rendered":"<p>I am using JQueryMobile for building website that works on Mobile, tablet, and desktop computers.<\/p>\n<p>As my application is an Ajax application. I wanted to control the browser URL in order to enable browser back\/forward button in my Ajax single-page application.<\/p>\n<p>The solution is really easy, whenever you want to change the location call<\/p>\n<p>location.hash = &#8220;456&#8221;;<\/p>\n<p>This will update browser URL as following:<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp;http:\/\/mysite.com\/myfolder\/index.html#456<\/p>\n<p>Of course passing your own application specific hash<\/p>\n<div>\n<\/div>\n<p>Whenever the user click back or forward, you need to handle the following event:<\/p>\n<p><span style=\"background-color: #d0e0e3;\">window.onhashchange = function () {<\/span><br \/>\n<span style=\"background-color: #d0e0e3;\"><br \/><\/span><br \/>\n<span style=\"background-color: #d0e0e3;\">&nbsp; &nbsp; &nbsp; &nbsp; \/\/Use location.hash and update your application display as you need<\/span><br \/>\n<span style=\"background-color: #d0e0e3;\"><br \/><\/span><br \/>\n<span style=\"background-color: #d0e0e3;\">};<\/span><\/p>\n<p>Until now everything is perfect. But if you are using JQM (JqueryMobile) you will face trouble.<br \/>\nJQM uses hash to manage the transition between multiple pages and dialogues. While doing so, it update the localtion.hash.<\/p>\n<p>The best workaround for this is to let JQM leave location.history alone. This can be easily done by:<\/p>\n<p><span style=\"background-color: #d0e0e3;\"><span style=\"white-space: pre;\"> <\/span>$.mobile.changePage(&#8220;#my-page&#8221;, {changeHash: false});<\/span><\/p>\n<p>As you see, the second parameter is an object with many option, what we need here is:<\/p>\n<p><span style=\"background-color: #d0e0e3;\">&nbsp; &nbsp; &nbsp; changeHash: false<\/span><\/p>\n<p>And viola, now you have full control of your location hash.<\/p>\n<p>What is a downside is that it will impose burden on you to control all page\/dialogue changes yourself. you can not simply use:<\/p>\n<p><span style=\"background-color: #d0e0e3;\">&nbsp; &nbsp; &nbsp; &nbsp; href=&#8221;#my_other_page&#8221;<\/span><\/p>\n<p>Because JQM will simply change it but will update browser hash.<\/p>\n<p>\nLinks:<br \/>\n<a href=\"https:\/\/api.jquerymobile.com\/jQuery.mobile.changePage\/\">https:\/\/api.jquerymobile.com\/jQuery.mobile.changePage\/<\/a><\/p>\n<div>\n<\/div>\n<div>From ahm507.blogspot.com<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I am using JQueryMobile for building website that works on Mobile, tablet, and desktop computers. As my application is an Ajax application. I wanted to control the browser URL in order to enable browser back\/forward button in my Ajax single-page application. The solution is really easy, whenever you want to change the location call location.hash [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/posts\/33"}],"collection":[{"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":0,"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"wp:attachment":[{"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}