Raymond Camden's Blog Rss

Getting URL parameters in a jQuery Mobile page

4

Posted in Mobile, Development, jQuery | Posted on 02-24-2012 | 5,300 views

While I could have sworn I blogged this before (in fact, Chrome popped autosuggested the title), I can't find the post and as I ran into this issue again today, I figured I'd better blog it before I forget again. So - the question is simple. Given that you can use the powerful jQuery Mobile framework to create dynamic applications, how can you create single pages that act differently depending on the URL parameters sent to them. So for example, you may have a list of art pieces that all link to artdisplay.html. You want to pass an art ID value in the URL so your location may look like so:

artdisplay.html?id=2

You can bind to the "pageshow" event easily enough and then fetch the URL parameter, but here is where things get tricky. Or at least they did for me. I was making use of window.location.href like so:

view plain print about
1$("#artdetailpage").live("pageshow", function(e) {
2 var query = window.location.search;
3 query = query.replace("?id=","");
4 //query is now an ID, do stuff with it...
5});

window.location.search returns just the query string portion of the URL. If I only have one parameter, then I can simply strip it out. (Note that it also returns the ?, which seems silly.)

This worked fine until I put the code onto a device via PhoneGap. All of a sudden window.location.search was empty. Given than this was a slightly different way of running the application versus just hitting in my browser, I searched for another solution.

Turns out that jQuery Mobile stores the URL of the page in a data-page parameter. This is documented here. If you switch to using $(this).data("url"), you will get the full URL, which means slightly more string parsing, but it seems to work in every situation, desktop or mobile. The pseudo-code above could then look like so:

view plain print about
1$("#artdetailpage").live("pageshow", function(e) {
2 var query = $(this).data("url").split("?")[1];;
3 query = query.replace("id=","");
4 //query is now an ID, do stuff with it...
5});

Comments

[Add Comment] [Subscribe to Comments]

This is a very handy tip! I figured out how to do this by picking apart your Simple Notes application. Actually, I've learned a whole lot of stuff from that app (not just this technique)!

Cheers and Happy Friday!
Yeah, I kinda figured this is something I've forgot/remembered multiple times. I'm hoping this blog entry helps it stick in my head.
Hey Ray, curious - have you tried Chris Coenraets getURLVars function (http://coenraets.org/blog/2011/10/sample-applicati...)? He states in the article it works for him in PhoneGap but many have only been able to recreate it in a browser. Once in PhoneGap - nothing. I assume the cause of failure is the reason you mention in this article - I'll be trying out getting the param through the page URL from now on! Thanks!
Nope, haven't tried it. Looks similar to code I had tried (and saw fail) too.