[转]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 的程序员总是很马虎的,所以,基站的传功功率不能独立设置 ...
随机推荐
- ROS nodelet 理解记录
发现网上许多的例子都是基于官网的例子,还需要做进一步的说明. 1. NODELET_DEBUG 是无法打印的信息的,需要使用NODELET_INFO NODELET_DEBUG("Addin ...
- 第一个SpringMVC程序(最简单的)
注册中央调度器,这个中央调度器就是org.springframework.web.servlet.DispatcherServlet这个类(web.xml servlet-name节点的名字,必须 ...
- winfrom强制退出程序(Application.Exit()与Environment.Exit(0))
Application.Exit():通知所有消息泵必须终止,并且在处理了消息以后关闭所有应用程序窗口(先停止线程(前台线程执行完毕,后台线程自动结束),然后终止进程) Environment.Exi ...
- Linux文件锁flock ,检测进程是否已经存在
在多个进程同时操作同一份文件的过程中,很容易导致文件中的数据混乱,需要锁操作来保证数据的完整性,这里介绍的针对文件的锁,称之为“文件锁”-flock. 头文件:#include<sys/fil ...
- Exception in thread "main" brut.androlib.err.UndefinedResObject: resource spec: 0x01030200
Exception in thread "main" brut.androlib.err.UndefinedResObject: resource spec: 0x01030200 ...
- 659. Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- Win10每次开机总是自动弹出MSN网址导航如何取消
Win10每次开机总是自动弹出MSN网址导航如何取消 近来有用户在升级Win10系统后,每次开机总是会自动弹出MSN中文网的网址导航.如果不想要开机打开MSN网址导航,那么应该怎么设置来取消呢?对此, ...
- 【转】PHP微信上传永久图片素材
$TOKEN="XXXX"; $file = "D:\www\weixin\game.jpg"; $data = array( 'media'=> new ...
- loj#6435. 「PKUSC2018」星际穿越(倍增)
题面 传送门 题解 我们先想想,在这个很特殊的图里该怎么走最短路 先设几个量,\(a_i\)表示\([a_i,i-1]\)之间的点都和\(i\)有边(即题中的\(l_i\)),\(l\)表示当前在计算 ...
- springboot jpa mongodb 整合mysql Field in required a bean of type that could not be found Failed to load ApplicationContext
1.完整报错 *************************** APPLICATION FAILED TO START *************************** Descripti ...