I've spent the better part of the past day becoming frustrated with trying to correctly pass parameters between dynamic pages in jQM, and posting here in the hopes that somebody can point me to the current correct answer. I'm using jQM 1.0.1 and jQuery 1.6.4.
My app has the following setup:
- About 6 external jQM HTML pages (i.e., NOT all in one jQM page, this is relevant because the hash discussion gets confused here)
- Basic flow:
- User logs in on login.html, gets passed to page A with "user" parameter (href="pageA.html?user=1")
- Page A sets "user" parameter into localstorage (since it's used on all other pages, and doesn't change) from the URL parameter
- Page A makes AJAX query to backend server for list of items, triggered by "pageshow" event, with user ID from localstorage
- Page A loads the list, giving each item a dynamically generated link with "item" parameter (href="pageB.html?item=1")
- User selects a link, goes to Page B
- Page B makes AJAX query to backend server for detailed item info, triggered by "pageshow" event, with user ID from localstorage and item ID from URL parameter
- Page B loads the detailed item info
In all cases, I'm using a small piece of JavaScript that pulls the parameters from the window.location.href that works well, and sets them into a variable that I can use elsewhere.
- function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
The problem I can't get around in the above scenario is that there seems to be a number of gotchas in various jQM events, and points where they trigger or don't, many of which are undocumented, and I can't figure out the right approach.
Specifically, in the above scenario, Page A succeeds at loading the dynamic info on the first load, however Page B doesn't. Reason? The "pageshow" event on Page B occurs before the window.location.href is changed. If I delay the script, using anything (like an alert or timer), then it works correctly. I found a bug report that was closed that says this was fixed a long time ago, but it obviously hasn't been (in 1.0.1 anyway). Documentation refers to pagebeforechange event, which also doesn't work because it's too early. Most of the other discussions about this topic are talking about passing parameters to internal pages, and the hash discussion gets involved, but I don't think that's relevant to my situation. The only example in the documentation also deals with this, and no example of external parameter passing exists that I've been able to find.
Anyway, I'm probably missing some nuance, or an event that I could reliably use to do this. Hoping somebody can point me in the right direction before I start doing some really hacky things to make it work (writing everything to localstorage from click events on every link - tedious).
Thanks in advance,
Mike
TL;DR - Need help finding either: 1) proper event to use where window.location.href will be reliably updated and I can parse parameters from it, or 2) better method for passing parameters to
external pages and parsing them
EDIT - realized that jQuery 1.7.1 wasn't supported with jQM 1.0.1, so swapped to 1.6.4, but that didn't fix the above issues.