[转]Passing data between pages in JQuery Mobile mobile.changePage
本文转自: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的更多相关文章
- AngularJS - Passing data between pages
You need to create a service to be able to share data between controllers. app.factory('myService', ...
- angular-ui-router state.go not passing data to $stateParams
app.js中定义了一个state如下,url接收一个id参数 $stateProvider.state("page.details", { url: "/details ...
- jQuery支持mobile的全屏水平横向翻页效果
这是一款支持移动手机mobile设备的jQuery全屏水平横向翻页效果插件. 该翻页插件能够使页面在水平方向上左右全屏翻动,它支持手机触摸屏,支持使用鼠标滚动页面. 整个页面过渡平滑,效果很不错. 在 ...
- jQuery Layer mobile 弹出层
layer mobile是为移动设备(手机.平板等webkit内核浏览器/webview)量身定做的弹层支撑,采用Native JavaScript编写,完全独立于PC版的layer,您需要按照场景选 ...
- [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 ...
- [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 ...
- [Angular 2] Passing data to components with 'properties'
Besides @Input(), we can also use properties on the @Component, to pass the data. import {Component, ...
- 3D Computer Grapihcs Using OpenGL - 07 Passing Data from Vertex to Fragment Shader
上节的最后我们实现了两个绿色的三角形,而绿色是直接在Fragment Shader中指定的. 这节我们将为这两个三角形进行更加自由的着色——五个顶点各自使用不同的颜色. 要实现这个目的,我们分两步进行 ...
- 【JZOJ3636】【BOI2012】Mobile(mobile)
Mission 著名的手机网络运营商Totalphone 修建了若干基站收发台,以用于把信号网络覆盖一条新建的高速公路.因为Totalphone 的程序员总是很马虎的,所以,基站的传功功率不能独立设置 ...
随机推荐
- jdbc--1 一些方法的封装
今日内容介绍1.JDBC2.DBUtils====================================================================1 JDBC概念和数据 ...
- GTXE_COMMON
http://forums.xilinx.com/xlnx/board/crawl_message?board.id=IMPBD&message.id=9657 If you are usin ...
- 工作中用的cobbler命令行
在使用cobbler服务器,从pxe启动虚机的时候,经常用到的cobbler命令行 1.查看注册信息 cobbler system report --name=test25 2.注册信息 cobble ...
- scala lambda 表达式 & spark RDD函数操作
形式:(参数)=> 表达式 [ 一种匿名函数 ] 例1:map(x => x._2) 解:x=输入参数,“=>” 右边是表达式(处理参数): x._2 : x变为(**,x,**. ...
- UISplitViewController
前言 大多数时候,iPhone.iPod 应用与 iPad 应用开发没有太大的区别,但是 iPad 的屏幕比 iPhone 大, 设计程序时可以充分利用 iPad 的大屏幕特点,例如 TabBar 和 ...
- vue添加新属性不更新原因
一: 在我们使用vue进行开发的过程中,可能会遇到一种情况:当生成vue实例后,当再次给数据赋值时,有时候并不会自动更新到视图上去: 当我们去看vue文档的时候,会发现有这么一句话:如果在实例创建之后 ...
- 洛谷P3178 [HAOI2015]树上操作(线段树)
题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 ...
- js数据类型基础
一.数据类型 数据类型包括:基本数据类型和引用数据类型 基本数据类型指的是简单的数据段,引用数据类型指的是有多个值构成的对象. 当我们把变量赋值给一个变量时,解析器首先要确认的就是这个值是基本类型值还 ...
- js计算每个月的总天数
js中相关日期的计算 var year = new Date().getFullYear(),//计算当前年份 month = new Date().getMonth()+1,//计算当前月份 dat ...
- sap程序下载
有时需要下载sap 中的程序 一屏一屏的拷贝非常麻烦 这里是在网上找的的一个下载工具 实际就是一个sap程序 建立好sap程序 把代码拷贝进去 在参数处输入程序名字 和下载位置 F8运 ...