[转]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 的程序员总是很马虎的,所以,基站的传功功率不能独立设置 ...
随机推荐
- 关于Spring注解配置的步骤
今天分享一下 关于Spring注解配置的流程 1 导包:如下图所示 2 书写User和Car类 代码如下 package cn.lijun.bean; public class Car { priv ...
- 用Pdg2.DLL解码PDG的境界
作者:马健邮箱:stronghorse_mj@hotmail.com发布:2008.08.03 一.入门级原理:按照<用BCB实现超星格式转换为BMP格式>中说的方法调用Pdg2.DLL接 ...
- 【zookeeper】
window下安装zookeeper三结点集群: 1:解压缩zookeeper压缩包:复制三分并且命名成:Server_A Server_B Server_C 2:拷贝conf目录下的文件zoo ...
- SqlServer压缩数据库日志
)--数据库名称 )--数据库日志文件名称 --替换成自己的文件名称 select @dbName='dbname' select @dbNamelog='dbname_log' ) set @sql ...
- Android源码树中C代码的编译
侯 Sir说:“源码之下,了无秘密.” 但有些秘密还是要搞起来了看得更真切,仅从静态代码的体位很难体会到运动时的妙处.因此环境搭好了,下一步就是调试.gdbserver搭配gdb的调试环境走得很顺利, ...
- iOS开发:setNeedsLayOut和setNeedsDisplay区别
1.layoutSubviews方法 1.1 根据苹果官方帮助文档对layoutSubviews方法的解释: 此方法用来重新定义子元素的位置和大小.当子类重写此方法,用来实现UI元素的更精确布局.如果 ...
- PyCharm中的Console调整字体大小
file-->settings-->Editor-->color Scheme-->Console Font --> size 调整大小
- Nginx——1.基础知识
Nginx——1.基础知识 作为高速.轻量.高性能等优点集于一身的服务器,Nginx在近些年迅速发展并不断扩大市场份额,甚至在最近其市场份额一举超过微软的IIS,跃身到第二位,仅次于Apache. 但 ...
- 51nod1455(dp)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1455 题意: 中文题诶~ 思路: dp 1 <= n, ...
- poj1753(高斯消元解mod2方程组)
题目链接:http://poj.org/problem?id=1753 题意:一个 4*4 的棋盘,初始时上面放满了黑色或白色的棋子.对 (i, j) 位置进行一次操作后 (i, j), (i + 1 ...