本文转自: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. HDU 3974 Assign the task (DFS+线段树)

    题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...

  2. C#和.NET Framework简介

    注:本文大部分借鉴了<果壳中的C#5.0权威指南>,小编也想根据这本书好好梳理一下C#. 序言:C#是一种通用的类型安全且面向对象的编程语言.这种语言的目标是提高程序员的生产力,为此,需要 ...

  3. ObjectARX环境搭建之vs2010+objectArx2012+AutoCAD2012

    ---------------------------------------------------------------------------------------------------- ...

  4. windows10系统连接蓝牙鼠标自动断开解决方案

    环境: Windows 10 企业版 2016 长期服务版 罗技M590 问题: 鼠标长时间未使用,会自动断开 解决步骤: 参考链接: https://zhidao.baidu.com/questio ...

  5. Apache虚拟主机-解惑篇

        有很多平时喜欢钻研的童鞋会发现,为什么有时候自己访问某XXse网站时,总是更新IP地址,内容却与以前一样.这个时候就要了解虚拟主机的概念了.了解这个概念,能够帮助运维同学,更内涵的隐藏自己的主 ...

  6. Python:raw_input 和 input用法

    转自:http://blog.csdn.net/kjing/article/details/7450146 Python input和raw_input的区别 使用input和raw_input都可以 ...

  7. Unity 动画系统 Animation和Animator等常用类

  8. freemarker 定义公共header

    <#--公共顶部--> <#macro header title="默认文字" keywords="默认文字" description=&qu ...

  9. 8.9zju集训日记

    和新队员的第一次比赛,前期开题方向基本正确,签到的速度比较快,中期读了旋转卡壳,矩阵和km的三道题目,都有一定的想法,但三个人意见不同没有往一个方向想,但其实旋转卡壳和km的题目思路几乎都对了,但是旋 ...

  10. day16 类之间的关系 特殊成员

    类与类之间的关系1.依赖关系(一个对象当另一个对象的参数)  关系最浅, 特殊成员: 1. 类名() 会自动调用 __init__() class Foo: def__init__(self, nam ...