I have built a mobile application using HTML5 & jquery mobile, I just need to go back to first page each time the back button is pressed.
Let's say I have these pages:
- Main
- Product
- Search
- Search Result
- About
Let's say I go:
Main --> Search --> Search Result --> Product, then I went to About page.
When going back, the history would be (if I'm on About page):
About --> Product --> Search Result --> Search --> Main
What I need is to go back directly to Main page each time I press back page and not to go through the history.
I tried to search for this in the last couple without any luck, and it is stopping me from finalizing my app.
I was trying for days to search for answer I came to this code:
jQuery(document).ready(function($) {
if (window.history && window.history.pushState) {
$(window).on('popstate', function() {
var hashLocation = location.hash;
var hashSplit = hashLocation.split("#!/");
var hashName = hashSplit[1];
if (hashName !== '') {
var hash = window.location.hash;
if (hash === '') {
window.location = 'index.html#mainScreen';
}
}
});
}
});
but still no luck
I also got this code:
$(window).on("navigate", function (event, data) {
var direction = data.state.direction;
if (direction == 'back') {
window.location = 'index.html#mainScreen';
}
});
It is going back to the mainScreen page if the direction is back, but it showing the last page before going back to the main page.