[转]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 的程序员总是很马虎的,所以,基站的传功功率不能独立设置 ...
随机推荐
- Java之封装特性
Java中的三大特性:继承,封装,多态: 其中封装概念:封装是把过程和数据包围起来,对数据的访问只能通过已定义的接口. 面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治.封装的 对象 ...
- com.fasterxml.jackson.databind.JavaType.isReferenceType
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-map ...
- HTML5游戏开发 PDF扫描版
很多从事Web前端开发的人对HTML总有些不满,比如需要手动检查和设计很多格式代码,不仅容易出错,而且存在大量重复.好在HTML5让我们看到了曙光.作为下一代Web开发标准,HTML5成为主流的日子已 ...
- Arcgis android10.2测试版中android.view.InflateException
最近2天总是有时出现 下面这个错误 android.view.InflateException: Binary XML file line #15: Error inflating class co ...
- FineUI从iis6迁移到iis7.5上遇到的奇葩事情
前天把一台旧服务器上的windows2003+iis6上的fineui项目迁移到了win7+iis7上面来了,没有编译,直接以源码方式运行. 本来运行的好好的,昨天下午在上面用vs2010打开了一下看 ...
- 根据GridView模板里的列名获取列索引
以前Insus.NET在写过一篇<在Gridview控件中根据Field Name来取得对应列索引> http://www.cnblogs.com/insus/archive/2010/0 ...
- Locust学习总结分享
简介: Locust是一个用于可扩展的,分布式的,性能测试的,开源的,用Python编写框架/工具,它非常容易使用,也非常好学.它的主要思想就是模拟一群用户将访问你的网站.每个用户的行为由你编写的py ...
- 大白话解说TCP/IP协议三次握手和四次挥手
背景 和女朋友异地恋一年多,为了保持感情我提议每天晚上视频聊天一次. 从好上开始,到现在,一年多也算坚持下来了. 问题 有时候聊天的过程中,我的网络或者她的网络可能会不好,视频就会卡住,听不到对方的声 ...
- [SinGuLaRiTy] 动态规划题目复习
[SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metr ...
- Linux配置国内的Yum源
因为Linux默认的yum源是国外的源,所以会有卡顿,缓慢的情况.而国内的Yum源相对速度较快,现在也比较成熟,所以给Linux更换国内Yum源是一个很好的选择. 1. 备份(备份之前需要yum i ...