Move this topic
Passing Parameters Between Pages
in jQuery Mobile
•
2 years ago
I'm attempting to pass parameters from one page (ex. with a list view of items, each with an id) to the sub-page corresponding to each item. I need to pass the id to the sub-page. I'm attempting to do this with get-style parameters resulting in the following URL:
.../index.html#item.html?id=1898432
I have a pageshow callback: $('[data-role=page]').live('pageshow') which triggers when the page is loaded. However, when this event fires, the window.location isn't updated to have the extra parameters in it.
I think there are two possibilities:
1. I'm doing it wrong. Perhaps sub-hash URLs are the way to do what I'm trying to do:
http://jquerymobile.com/demos/1.0a2/#docs/pages/docs-navmodel.html
2. The event should fire after window.location is updated to reflect the URL with parameters.
11
Replies(26)
Re: Passing Parameters Between Pages
2 years ago
You should be able to bypass the problem by desactivating the ajaxload page parameter. I'd be glad to find a correct way of doing this though for now I need to do external links each time I have to capture a get element with js...
Leave a comment on m4nu56's reply
Re: Passing Parameters Between Pages
2 years ago
That sort of defeats part of the purpose of using jQuery Mobile doesn't it?
Note: this is already being discussed on the bugtracker https://github.com/jquery/jquery-mobile/issues/issue/450/
Also see my blog post: http://www.borismus.com/jquery-mobile-hacker-news/
Leave a comment on borismus's reply
Re: Passing Parameters Between Pages
2 years ago
Thank's for your help @borismus; in the end you used the setTimeout method in order to get the good parameter from the window.location or have you succeeded using $.mobile.changePage ?
Re: Passing Parameters Between Pages
2 years ago
Still using setTimeout. I think it has a good chance of getting fixed in the next version, since the issue is marked critical in the bug tracker.
Leave a comment on m4nu56's reply
Re: Passing Parameters Between Pages
2 years ago
Here is the solution I am using -
Do not use URL args but use a data attr like this:
<li><a href='#list' data-identity='123'>Jon Doe</a></li>
Get the data in a click event and create the appropriate html like this
$("a[href=#list]").live(
"click",
function(e) {
navIdentity = $(this).data("identity");
$("#listbody").html( "<div>" + navIdentity + "</div>" );
$("#list").page();
}
);
The problem I have is that the second time I go to one of the pages the HTML is not styled correctly so I am looking into how to "refresh" a whole page.
Thanks,
Sean
Do not use URL args but use a data attr like this:
<li><a href='#list' data-identity='123'>Jon Doe</a></li>
Get the data in a click event and create the appropriate html like this
$("a[href=#list]").live(
"click",
function(e) {
navIdentity = $(this).data("identity");
$("#listbody").html( "<div>" + navIdentity + "</div>" );
$("#list").page();
}
);
The problem I have is that the second time I go to one of the pages the HTML is not styled correctly so I am looking into how to "refresh" a whole page.
Thanks,
Sean
Leave a comment on seanmoore's reply
Re: Passing Parameters Between Pages
2 years ago
Well for now I decided to open those pages where I need to get a parameter with a rel="external" so that I can record the get parameter in the url..
I'd be glad to learn a better way of doing it without using PHP though...
Leave a comment on m4nu56's reply
Re: Passing Parameters Between Pages
2 years ago
Don't know if this will help anyone else than me but since I know that the only one who is going to open my queried url's is jQuery mobile. So for url's containing parameters my href looks like this instead:
<a href='javascript:$.mobile.changePage("Test?action=myactionr&sub=subaction&woid=12312", "slide")'>asd</a>
Leave a comment on eriksson.viktor's reply
Re: Passing Parameters Between Pages
2 years ago
You could use sessionStorage to move your Data between Pages,instead of the Url
<a href="item.html" onClick="sessionStorage.id=1898432">Item 1898432</a>
<a href="item.html" onClick="sessionStorage.id=1898432">Item 1898432</a>
Re: Passing Parameters Between Pages
1 year ago
I agree I tried the to pass through URL but it didn't really work for me. Since I had several pages that you can circle among then you'd loose your URL once you came back to that page again. The best and safest practice I believe is using sessionstorage.
sessionstorage.setItem(keystring, value);
and then to retrieve it, sessionstorage.getItem(keystring);
more about it can be read at: http://code.google.com/p/sessionstorage/
Leave a comment on andorxor's reply
Re: Passing Parameters Between Pages
2 years ago
Passing data using GET vars seems to be working now. I was going to write a blog post about it and so I started testing last night:
Click the "passing query vars" button, then click either listview item. Use Firefox and have the Firebug console window open to see output.
Re: Passing Parameters Between Pages
2 years ago
Yes it's working, but JQuery Mobile create one page in the DOM (i.e div with data-role="page") by query . Each pages has a data-url like "foo.html?query"... for me is not really satisfactory
Leave a comment on commadelimited's reply
Re: Passing Parameters Between Pages
2 years ago
i'm using something like this:
function MyChangePage(page, args)
{
$.mobile.changePage(page);
$.mobile.updateHash(page, true);
$(page).trigger("callback", args);
}
// bind callback that will be triggered after a pageshow event
$("#pagename").bind("callback", function(e, args) {
alert(args.mydata);
});
// change to a page, and pass along parameters to the callback function
MyChangePage("#pagename", { mydata: "hello" });
// a slightly more complete example
(html)
<div id="page1" data-role="page">
...
<ul>
<li><a my-data="1" href="javascript:void(0)">Option 1</a></li>
<li><a my-data="2" href="javascript:void(0)">Option 2</a></li>
<li><a my-data="3" href="javascript:void(0)">Option 3</a></li>
<ul>
</div>
<div id="page2" data-role="page">
<div class="result"></div>
</div>
(js)
$("#page2").bind("callback", function(e, args) {
$("#page2 .result").text("You selected option: " + args.sel);
});
$("#page1 ul").delegate("a", "click", function() {
var attr = $(this).attr("my-data");
MyChangePage("#page2", { "sel": attr });
});
function MyChangePage(page, args)
{
$.mobile.changePage(page);
$.mobile.updateHash(page, true);
$(page).trigger("callback", args);
}
// bind callback that will be triggered after a pageshow event
$("#pagename").bind("callback", function(e, args) {
alert(args.mydata);
});
// change to a page, and pass along parameters to the callback function
MyChangePage("#pagename", { mydata: "hello" });
// a slightly more complete example
(html)
<div id="page1" data-role="page">
...
<ul>
<li><a my-data="1" href="javascript:void(0)">Option 1</a></li>
<li><a my-data="2" href="javascript:void(0)">Option 2</a></li>
<li><a my-data="3" href="javascript:void(0)">Option 3</a></li>
<ul>
</div>
<div id="page2" data-role="page">
<div class="result"></div>
</div>
(js)
$("#page2").bind("callback", function(e, args) {
$("#page2 .result").text("You selected option: " + args.sel);
});
$("#page1 ul").delegate("a", "click", function() {
var attr = $(this).attr("my-data");
MyChangePage("#page2", { "sel": attr });
});
Leave a comment on locksmith's reply
Leave a comment on project9's reply
Re: Passing Parameters Between Pages
1 years ago
"Yes it's working, but JQuery Mobile create one page in the DOM (i.e div with data-role="page") by query . Each pages has a data-url like "foo.html?query"... for me is not really satisfactory"
[url=http://www.tiffanycooutlet.org]Tiffany and co[/url]'s every ornaments has its unique beautiful luster.
Agreed to
[url=http://www.tiffanycooutlet.org]Tiffany and co[/url]'s every ornaments has its unique beautiful luster.
Leave a comment on skym04's reply
Re: Passing Parameters Between Pages
1 years ago
I have used "localStorage" for passing data between pages:
Leave a comment on Thomas Amsler's reply
Re: Passing Parameters Between Pages
1 years ago
Soo i was having the same issue... The problem occurs only if you try to grab a parameter during document ready. Document ready actually triggers before page show so technically the URL is still the old URL. Try:
- function getUrlVars(){
- var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
- for(var i = 0; i < hashes.length; i++){
- hash = hashes[i].split('=');
- vars.push(hash[0]);
- vars[hash[0]] = hash[1];
- }
- }
- $('div').live('pagebeforeshow',function(event, ui){
- getUrlVars();
- alert(vars["youvariablenamehere"]);
- }
Leave a comment on soparrissays's reply
Re: Passing Parameters Between Pages
1 year ago
so there's still no real solution to this or it is out dated?
I also kinda frustrated that the examples provided are not "beginners friendly" ,i however respect any commitment to solve any issue.
I also kinda frustrated that the examples provided are not "beginners friendly" ,i however respect any commitment to solve any issue.
Leave a comment on osc2nuke's reply
Re: Passing Parameters Between Pages
1 year ago
Well, in my experience, this had been fixed in 1.0-b2. With that version, parameters were available during 'pageshow' events.
As of 1.0-RC1, the behaviour has changed and it is not clear where the problem lays. But, code that used to work has started failing.
Leave a comment on jpfiset's reply
Re: Passing Parameters Between Pages
1 year ago
In the instance where a parametrized page refers to another parametrized page, the routine to retrieve parameters presented above fails. The reason is a change in the way window.location is updated. I notice this change first in RC1.
In 1.0-b02, a transition between a parametrized page and another parametrized page yielded the following:
window.location.href during the 'pagebeforeshow' event:
The same transition in 1.0-RC1 yields the following:
window.location.href during the 'pagebeforeshow' event:
The problem in RC1 is that the temporary URL found in window.location.href contains two '?' characters. Therefore, an update to the routine presented above is required:
- function getUrlVars(){
- var href = window.location.href;
- var hashIndex = href.indexOf('#');
- if( hashIndex >= 0 ) {
- href = href.slice(href.indexOf('#')+1);
- };
- var hashes = href.slice(href.indexOf('?') + 1).split('&');
- for(var i = 0; i < hashes.length; ++i){
- hash = hashes[i].split('=');
- vars.push(hash[0]);
- vars[hash[0]] = hash[1];
- };
- };
The same method for accessing the parameters can be used and is replicated here for completeness:
- $('div').live('pagebeforeshow',function(event, ui){
- getUrlVars();
- alert(vars['youvariablenamehere']);
- });
Re: Passing Parameters Between Pages
1 year ago
I also kinda frustrated that the examples provided are not "beginners friendly"
Leave a comment on jpfiset's reply
Re: Passing Parameters Between Pages
1 year ago
Works for me (including animated transition to same page) as long as I keep my pages in separate HTML files. Example here: http://coenraets.org/blog/2011/10/sample-application-with-jquery-mobile-and-phonegap/
Leave a comment on ccoenraets's reply
Re: Passing Parameters Between Pages
1 year ago
I'm having the same issue with RC.
ccoenraets: I did that before but another bug forced me to put all div:s in the same page. When I had the pages in separate files the offline mode in iPhone didn't work.
ccoenraets: I did that before but another bug forced me to put all div:s in the same page. When I had the pages in separate files the offline mode in iPhone didn't work.
Leave a comment on dd_one's reply
Re: Passing Parameters Between Pages
1 year ago
@ccoenraets : I tried your sample and I like your elegant way of passing parameters.
However there is a bug when linking the following :
Root > overview(id1) > some page > overview(id2)
(Note that I am using jquery mobile 1.0.1)
The problem comes from the way JQM generates hashes for sub-pages : it concatenates the hashes altogether with a "#". If you try your method with the example above, you will see that the second overview page will display the overview of id1 (instead of id2).
The simple fix is to slice using the LAST index of "? "in the href before starting to parse. The correct method that worked for me :
-
function getUrlVars() {
var vars = [], hash;
var href = window.location.href;
var queryUrl =href.slice(href.lastIndexOf('?') + 1);
var hashes = queryUrl.split('&');
for(var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
Hope it could help!
Christophe
Leave a comment on cfondacci's reply
Re: Passing Parameters Between Pages
1 year ago
those who struggle with the hashtag thing may try this:
say you want to pass some var to an internal page. Let it be #category.
and the link that goes to that page is app.htm#category?burgers
$( document ).delegate("#category", "pageshow", function() {
var getHash = window.location.hash.split("?");
console.log(getHash[1]); // will display burgers
});
unfortunately, it doesnt work on my iphone 4s
Leave a comment on mikekoro's reply
Re: Passing Parameters Between Pages
5 months ago
I am not sure if this bug was fixed already, it probably was, because my code worked very easily just by passing the parameters in the url and retrieving them with GET
Dynamically created the url like this:
- echo '<a href="share_routine.php?routine_id='.$row["routine_id"].'" data-role="button" data-icon="forward" data-inline="true" data-theme="e">Share Routine</a> ';
Retrieved value share_routine.php:
- $routine_id=$_GET["routine_id"];
Leave a comment on cristina.taxicreative's reply
Re: Passing Parameters Between Pages
5 months ago
project9's suggestion worked for me, not sure why just yet, here's the function:
- function getUrlVars() {
- var vars = [], hash;
- var href = window.location.href;
- var queryUrl =href.slice(href.lastIndexOf( '?' ) + 1);
- var hashes = queryUrl.split( '&' );
- for ( var i = 0; i < hashes.length; i++) {
- hash = hashes[i].split( '=' );
- vars.push(hash[0]);
- vars[hash[0]] = hash[1];
- }
- return vars;
-
}
In my particular case, I was using a similar getUrVarsl() function and I had a list of businesses that, when clicked, would go to a page like business-profile.html?id=272, and that page would get a JSON response from the server of the business profile with the corresponding id in the url. I was using the code snippet from the link above since it has been used in numerous tutorial and code samples, so I assumed it worked like a charm.
At first it seemed to work just fine, but when a list got bigger, the pageshow event seemed to fire before the URL actually updated, so getUrlVars()['id'] returned the proper id toward the top of the list, but returned undefined as you worked your way down the list. The function pasted above fixed the problem for me.
If anyone can point out why it is that this works, bonus!
Leave a comment on kylewhenderson's reply
Change topic type
Link this topic
Provide the permalink of a topic that is related to this topic
Reply to borismus's question
{"z19376672":[14737000003358391],"z7116959":[14737000002821508],"z5921901":[14737000003727877],"z6225710":[14737000002413442],"z5645413":[14737000002413107],"z5494060":[14737000002008202],"z7113498":[14737000002521211],"z5551931":[14737000002761801,14737000002767152],"z4373358":[14737000001612094,14737000001649247,14737000001669176],"z5514004":[14737000002010401],"z8813652":[14737000003145097],"z7728094":[14737000002775440],"z5273600":[14737000002008322],"z4708792":[14737000001648385],"z5558767":[14737000002032250],"z5493180":[14737000002005762],"z7685681":[14737000002764436,14737000002766554],"z4903658":[14737000001645925,14737000001649273,14737000001672003],"z1486611":[14737000002416178],"z23618183":[14737000003740184],"z7504359":[14737000002695668]}
Statistics
- 26 Replies
- 63855 Views
- 8 Followers



