本文转自:http://ramkulkarni.com/blog/passing-data-between-pages-in-jquery-mobile/

I am working on a JQuery Mobile application which is based on single page templatedesign. 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 the application and I switch between pages using changePage. One of the issues I struggled a bit initially was, how to pass data between pages? The signature of changePage is

$.mobile.changePage(to,options)

‘to’ argument is URL of the page to which you want to change to. ‘options’ is an object with many different keys. One of the keys is ‘data’ and the documentation says – “data (object or string, default: undefined) The data to send with an Ajax page request.”.

So I could pass data to the page I want to change to either by passing URL parameters directly, or as ‘data’ in ‘options’ in changePage i.e.

$.mobile.changePage("newPage.html?param1=value1");

OR

$.mobile.changePage("newPage.html", {data:{param1:'value1'}});

I thought passing values in ‘data’ in ‘options’ is cleaner solution. But the problem is that JQuery Mobile does not help you to retrieve ‘data’ in the new page. I was hoping that any of the page event callback functions of JQuery Mobile, like pageload, pagechange etc., would pass ‘data’ that was passed from invoking page as one of the arguments. But it does not.

BTW, changePage function also appends parameter passed as ‘data’ 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’t have. However there is a JQuery plug-in which does this – JQuery URL Parser . To use this plug-in, include jquery.url.js after you include jquery file -

<script src="jquery-1.7.2.js"></script>
<script src="jquery.mobile-1.1.0.js"></script>
<script src="jquery.url.js"></script>

Then you can retrieve URL params as follows -

<div data-role="page" id="newPageId">
<script type="text/javascript">
$("#newPageId").on("pageshow", onPageShow); function onPageShow(e,data)
{
var url = $.url(document.location); var param1 = url.param("param1");
}
</script>
</div>

When passing data values, JQuery Mobile replaces spaces (in values) with ‘+’, so you will have to replace them when you get param values. Or you can use encodeURIComponent and decodeURIComponent e.g.

in the page where you are calling changePage -

$.mobile.changePage("newPage.html",{data:{param1:encodeURIComponent('value with spaces')}});

And in the page where you want to retrieve param values -

var url = $.url(document.location);

var param1 = url.param("param1");
if (param1 != undefined)
param1 = decodeURIComponent(param1);

-Ram Kulkarni

Update : I was asked to provide a complete example for the scenario I discussed in this post. So I have posted a demo here.

[转]Passing data between pages in JQuery Mobile mobile.changePage的更多相关文章

  1. AngularJS - Passing data between pages

    You need to create a service to be able to share data between controllers. app.factory('myService', ...

  2. angular-ui-router state.go not passing data to $stateParams

    app.js中定义了一个state如下,url接收一个id参数 $stateProvider.state("page.details", { url: "/details ...

  3. jQuery支持mobile的全屏水平横向翻页效果

    这是一款支持移动手机mobile设备的jQuery全屏水平横向翻页效果插件. 该翻页插件能够使页面在水平方向上左右全屏翻动,它支持手机触摸屏,支持使用鼠标滚动页面. 整个页面过渡平滑,效果很不错. 在 ...

  4. jQuery Layer mobile 弹出层

    layer mobile是为移动设备(手机.平板等webkit内核浏览器/webview)量身定做的弹层支撑,采用Native JavaScript编写,完全独立于PC版的layer,您需要按照场景选 ...

  5. [React Native] Passing data when changing routes

    The way you make HTTP requests in React Native is with the Fetch API. In this video we'll talk about ...

  6. [Angular 2] Passing data to components with @Input

    @Input allows you to pass data into your controller and templates through html and defining custom p ...

  7. [Angular 2] Passing data to components with 'properties'

    Besides @Input(), we can also use properties on the @Component, to pass the data. import {Component, ...

  8. 3D Computer Grapihcs Using OpenGL - 07 Passing Data from Vertex to Fragment Shader

    上节的最后我们实现了两个绿色的三角形,而绿色是直接在Fragment Shader中指定的. 这节我们将为这两个三角形进行更加自由的着色——五个顶点各自使用不同的颜色. 要实现这个目的,我们分两步进行 ...

  9. 【JZOJ3636】【BOI2012】Mobile(mobile)

    Mission 著名的手机网络运营商Totalphone 修建了若干基站收发台,以用于把信号网络覆盖一条新建的高速公路.因为Totalphone 的程序员总是很马虎的,所以,基站的传功功率不能独立设置 ...

随机推荐

  1. 冒泡排序的PHP实现

    定义: 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成. 排序思想:(从后往前) 比较相邻的 ...

  2. EIP-uni-app框架-建立项目

    uni-app介绍 uni-app 是一个使用 Vue.js 开发跨平台应用的前端框架,开发者编写一套代码,可编译到iOS.Android.H5.小程序等多个平台.uni-app在跨端数量,扩展能力, ...

  3. namespace/symbol/:keyword/::keyword in Clojure

    此文已由作者张佃鹏授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.关键字(:keyword/::keyword):   最近在学习使用clojure.spec(验证数据的 ...

  4. Centos7环境下FastRunner前端(FasterWeb)部署

    FastRunner前端安装 1.安装和创建Python虚拟环境 安装virtualenvwrapper 2.拉取代码 cd ~ # 环境当前用户home目录 git clone git@github ...

  5. Python学习第三方库Requests: 让 HTTP 服务人类

    转自官方文档:http://cn.python-requests.org/zh_CN/latest/ 快速上手 http://cn.python-requests.org/zh_CN/latest/u ...

  6. nginx: [emerg] directive "location" has no opening "{" in /usr/local/nginx//conf/nginx.conf:75

    1.报错:[emerg]directive "location" has no opening "{" in ..... 解决方法: 由于对应行或者附近行的“{ ...

  7. linux内核中的##__VA_ARGS__有什么作用?

    答:   1.__VA_ARGS__是一个可变参数宏: 2. 当可变参数的个数为0时,这里的##起到把前面多余的","去掉的作用,否则会编译出错: 3. 示例 3.1 #defin ...

  8. PHP实现微信公众平台开发—基础篇

    PHP实现微信公众平台开发—基础篇 2 1课程介绍 2 2. 微信公众号账号介绍的申请流程详解 2 2.1微信三角色关系 2 2.2信息流的过程 2 2.4微信公众账号的认证 3 2.5订阅号的申请流 ...

  9. cron定时备份数据库

    1.定时备份数据库 shell 脚本 #!/bin/bash # export and backup the abgent_web database.sql mysqldump -uusername ...

  10. kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)

    After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...