Move this topic
Passing Parameters Between Pages
in jQuery Mobile
•
1 year 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(22)
Re: Passing Parameters Between Pages
1 year 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
1 year 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
1 year 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
1 year 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
1 year 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
1 year 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
1 year 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
1 year 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
5 months 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
1 year 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
1 year 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
1 year 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
8 months 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
8 months ago
I have used "localStorage" for passing data between pages:
Leave a comment on Thomas Amsler's reply
Re: Passing Parameters Between Pages
8 months 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
4 months 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
4 months 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
4 months 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
4 months 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
4 months 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
3 months 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
Change topic type
Link this topic
Provide the permalink of a topic that is related to this topic
Reply to borismus's question
{"z7116959":[14737000002821508],"z6225710":[14737000002413442],"z5645413":[14737000002413107],"z5494060":[14737000002008202],"z7113498":[14737000002521211],"z5551931":[14737000002761801,14737000002767152],"z4373358":[14737000001612094,14737000001649247,14737000001669176],"z5514004":[14737000002010401],"z7728094":[14737000002775440],"z5273600":[14737000002008322],"z4708792":[14737000001648385],"z5558767":[14737000002032250],"z5493180":[14737000002005762],"z7685681":[14737000002764436,14737000002766554],"z4903658":[14737000001645925,14737000001649273,14737000001672003],"z1486611":[14737000002416178],"z7504359":[14737000002695668]}
Statistics
- 22 Replies
- 16611 Views
- 8 Followers



