{"id":311,"date":"2012-06-03T14:16:47","date_gmt":"2012-06-03T08:46:47","guid":{"rendered":"http:\/\/ramkulkarni.com\/blog\/?p=311"},"modified":"2012-06-03T14:16:47","modified_gmt":"2012-06-03T08:46:47","slug":"passing-data-between-pages-in-jquery-mobile","status":"publish","type":"post","link":"http:\/\/ramkulkarni.com\/blog\/passing-data-between-pages-in-jquery-mobile\/","title":{"rendered":"Passing data between pages in JQuery Mobile"},"content":{"rendered":"<p>I am working on a JQuery Mobile application which is based on <a href=\"http:\/\/jquerymobile.com\/demos\/1.1.0\/docs\/pages\/page-template.html\" target=\"_blank\">single page template<\/a> design. In this design, there is one main page in the application and all other pages are loaded by JQuery using AJAX and content is embedded into the DOM of the main page.<\/p>\n<p>I have a couple of pages in the application and I switch between pages using <a href=\"http:\/\/jquerymobile.com\/demos\/1.1.0\/docs\/api\/methods.html\" target=\"_blank\">changePage<\/a>. One of the issues I struggled a bit initially was, how to pass data between pages? The signature of changePage is<br \/>\n<!--more--><\/p>\n<pre style=\"color: #000000; background: #f1f0f0;\">$<span style=\"color: #806030;\">.<\/span>mobile<span style=\"color: #806030;\">.<\/span>changePage<span style=\"color: #806030;\">(<\/span>to<span style=\"color: #806030;\">,<\/span>options<span style=\"color: #806030;\">)<\/span><\/pre>\n<p>&#8216;to&#8217; argument is URL of the page to which you want to change to. &#8216;options&#8217; is an object with many different keys. One of the keys is &#8216;data&#8217; and the documentation says &#8211; &#8220;data\u00a0(<em>object<\/em>\u00a0or string, default: undefined) The data to send with an Ajax page request.&#8221;.<\/p>\n<p>So I could pass data to the page I want to change to either by passing URL parameters directly, or as &#8216;data&#8217; in &#8216;options&#8217; in changePage i.e.<\/p>\n<pre style=\"color: #000000; background: #f1f0f0;\">$<span style=\"color: #806030;\">.<\/span>mobile<span style=\"color: #806030;\">.<\/span>changePage<span style=\"color: #806030;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #e60000;\">newPage.html?param1=value1<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #806030;\">)<\/span><span style=\"color: #806030;\">;<\/span><\/pre>\n<p>OR<\/p>\n<pre style=\"color: #000000; background: #f1f0f0;\">$<span style=\"color: #806030;\">.<\/span>mobile<span style=\"color: #806030;\">.<\/span>changePage<span style=\"color: #806030;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #e60000;\">newPage.html<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #806030;\">,<\/span> <span style=\"color: #806030;\">{<\/span>data<span style=\"color: #806030;\">:<\/span><span style=\"color: #806030;\">{<\/span>param1<span style=\"color: #806030;\">:<\/span><span style=\"color: #e60000;\">'value1'<\/span><span style=\"color: #806030;\">}<\/span><span style=\"color: #806030;\">}<\/span><span style=\"color: #806030;\">)<\/span><span style=\"color: #806030;\">;<\/span><\/pre>\n<p>I thought passing values in &#8216;data&#8217; in &#8216;options&#8217; is cleaner solution. But the problem is that JQuery Mobile does not help you to retrieve &#8216;data&#8217; in the new page. I was hoping that any of the page event callback functions of JQuery Mobile, like pageload, pagechange etc., would pass &#8216;data&#8217; that was passed from invoking page as one of the arguments. But it does not.<\/p>\n<p>BTW, changePage function also appends parameter passed as &#8216;data&#8217; to the page URL, so in that sense above two options are the same. So you will have to handle retrieving data yourself. I thought either JQuery or JQuery Mobile would have convenience functions to retrieve URL parameter, but surprisingly they don&#8217;t have. However there is a JQuery plug-in which does this &#8211; <a href=\"https:\/\/github.com\/allmarkedup\/jQuery-URL-Parser\" target=\"_blank\">JQuery URL Parser<\/a> . To use this plug-in, include jquery.url.js after you include jquery file &#8211;<\/p>\n<pre style=\"color: #000000; background: #f1f0f0;\"><span style=\"color: #806030;\">&lt;<\/span>script src<span style=\"color: #806030;\">=<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #e60000;\">jquery-1.7.2.js<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #806030;\">&gt;<\/span><span style=\"color: #806030;\">&lt;<\/span><span style=\"color: #806030;\">\/<\/span>script<span style=\"color: #806030;\">&gt;<\/span>\n<span style=\"color: #806030;\">&lt;<\/span>script src<span style=\"color: #806030;\">=<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #e60000;\">jquery.mobile-1.1.0.js<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #806030;\">&gt;<\/span><span style=\"color: #806030;\">&lt;<\/span><span style=\"color: #806030;\">\/<\/span>script<span style=\"color: #806030;\">&gt;<\/span>\n<span style=\"color: #806030;\">&lt;<\/span>script src<span style=\"color: #806030;\">=<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #e60000;\">jquery.url.js<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #806030;\">&gt;<\/span><span style=\"color: #806030;\">&lt;<\/span><span style=\"color: #806030;\">\/<\/span>script<span style=\"color: #806030;\">&gt;<\/span><\/pre>\n<p>Then you can retrieve URL params as follows &#8211;<\/p>\n<pre style=\"color: #000000; background: #f1f0f0;\"><span style=\"color: #806030;\">&lt;<\/span>div data<span style=\"color: #806030;\">-<\/span>role<span style=\"color: #806030;\">=<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #e60000;\">page<\/span><span style=\"color: #800000;\">\"<\/span> id<span style=\"color: #806030;\">=<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #e60000;\">newPageId<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #806030;\">&gt;<\/span>\n    <span style=\"color: #806030;\">&lt;<\/span>script type<span style=\"color: #806030;\">=<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #e60000;\">text\/javascript<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #806030;\">&gt;<\/span>\n        $<span style=\"color: #806030;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #e60000;\">#newPageId<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #806030;\">)<\/span><span style=\"color: #806030;\">.<\/span>on<span style=\"color: #806030;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #e60000;\">pageshow<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #806030;\">,<\/span> onPageShow<span style=\"color: #806030;\">)<\/span><span style=\"color: #806030;\">;<\/span>\n\n        <span style=\"color: #400000; font-weight: bold;\">function<\/span> onPageShow<span style=\"color: #806030;\">(<\/span>e<span style=\"color: #806030;\">,<\/span>data<span style=\"color: #806030;\">)<\/span>\n        <span style=\"color: #806030;\">{<\/span>\n            <span style=\"color: #400000; font-weight: bold;\">var<\/span> url <span style=\"color: #806030;\">=<\/span> $<span style=\"color: #806030;\">.<\/span>url<span style=\"color: #806030;\">(<\/span>document<span style=\"color: #806030;\">.<\/span>location<span style=\"color: #806030;\">)<\/span><span style=\"color: #806030;\">;<\/span>\n\n            <span style=\"color: #400000; font-weight: bold;\">var<\/span> param1 <span style=\"color: #806030;\">=<\/span> url<span style=\"color: #806030;\">.<\/span>param<span style=\"color: #806030;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #e60000;\">param1<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #806030;\">)<\/span><span style=\"color: #806030;\">;<\/span>\n        <span style=\"color: #806030;\">}<\/span>\n    <span style=\"color: #806030;\">&lt;<\/span><span style=\"color: #806030;\">\/<\/span>script<span style=\"color: #806030;\">&gt;<\/span>\n<span style=\"color: #806030;\">&lt;<\/span><span style=\"color: #806030;\">\/<\/span>div<span style=\"color: #806030;\">&gt;<\/span><\/pre>\n<p>When passing data values, JQuery Mobile replaces spaces (in values) with &#8216;+&#8217;, so you will have to replace them when you get param values. Or you can use encodeURIComponent and decodeURIComponent e.g.<\/p>\n<p>in the page where you are calling changePage &#8211;<\/p>\n<pre style=\"color: #000000; background: #f1f0f0;\">$<span style=\"color: #806030;\">.<\/span>mobile<span style=\"color: #806030;\">.<\/span>changePage<span style=\"color: #806030;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #e60000;\">newPage.html<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #806030;\">,<\/span><span style=\"color: #806030;\">{<\/span>data<span style=\"color: #806030;\">:<\/span><span style=\"color: #806030;\">{<\/span>param1<span style=\"color: #806030;\">:<\/span>encodeURIComponent<span style=\"color: #806030;\">(<\/span><span style=\"color: #e60000;\">'value with spaces'<\/span><span style=\"color: #806030;\">)<\/span><span style=\"color: #806030;\">}<\/span><span style=\"color: #806030;\">}<\/span><span style=\"color: #806030;\">)<\/span><span style=\"color: #806030;\">;<\/span><\/pre>\n<p>And in the page where you want to retrieve param values &#8211;<\/p>\n<pre style=\"color: #000000; background: #f1f0f0;\"><span style=\"color: #400000; font-weight: bold;\">var<\/span> url <span style=\"color: #806030;\">=<\/span> $<span style=\"color: #806030;\">.<\/span>url<span style=\"color: #806030;\">(<\/span>document<span style=\"color: #806030;\">.<\/span>location<span style=\"color: #806030;\">)<\/span><span style=\"color: #806030;\">;<\/span>\n\n<span style=\"color: #400000; font-weight: bold;\">var<\/span> param1 <span style=\"color: #806030;\">=<\/span> url<span style=\"color: #806030;\">.<\/span>param<span style=\"color: #806030;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #e60000;\">param1<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #806030;\">)<\/span><span style=\"color: #806030;\">;<\/span>\n<span style=\"color: #400000; font-weight: bold;\">if<\/span> <span style=\"color: #806030;\">(<\/span>param1 <span style=\"color: #806030;\">!=<\/span> undefined<span style=\"color: #806030;\">)<\/span>\n    param1 <span style=\"color: #806030;\">=<\/span> decodeURIComponent<span style=\"color: #806030;\">(<\/span>param1<span style=\"color: #806030;\">)<\/span><span style=\"color: #806030;\">;<\/span><\/pre>\n<p>-Ram Kulkarni<\/p>\n<p><strong>Update :<br \/>\n<\/strong>I was asked to provide a complete example for the scenario I discussed in this post. So I have posted a demo <a title=\"Demo - Passing data between pages in JQM\" href=\"http:\/\/ramkulkarni.com\/temp\/2012-06-03\/\" target=\"_blank\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am working on a JQuery Mobile application which is based on single page template design. In this design, there is one main page in the application and all other pages are loaded by JQuery using AJAX and content is embedded into the DOM of the main page. I have a couple of pages in &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/ramkulkarni.com\/blog\/passing-data-between-pages-in-jquery-mobile\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Passing data between pages in JQuery Mobile&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[30,1],"tags":[29,7,31],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2g9O8-51","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/311"}],"collection":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/comments?post=311"}],"version-history":[{"count":0,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/311\/revisions"}],"wp:attachment":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/media?parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/categories?post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/tags?post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}