I'm using jquery mobile with phonegap. My app has two pages: login page and list page. When login successfully, the user will go to the list page. Afterwards, when they press the back button on their phone (android), they will go back to the login page. I don't want behavior like this. What I want is to exit the app.
As I answered in this question: page hash and backbutton issue phonegap+Jquery
You can change pages without keeping them in browser history like so:
$.mobile.changePage('#page', {reverse: false, changeHash: false});
Unfortunately, I didn't manage to prevent the initial page from staying in browser history, so I used a workaround:
Page layout:
<body>
<!-- page_1 before page_loading in source -->
<div data-role="page" id="page_1">
</div>
<!-- page_loading will be shown first -->
<div data-role="page" id="page_loading">
<div data-role="content">
<h1 >
<b>welcome</b>
</h1>
</div>
</div>
<div data-role="page" id="page_2">
</div>
</body>
jQuery:
function onBodyLoad()
{
//go to page_loading before deviceready without keeping it in browser history
$.mobile.changePage('#page_loading', {reverse: false, changeHash: false});
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady()
{
//your initialization code here...
//now go to page_1 without keeping it in browser history since the actual first page was #page_1 already
$.mobile.changePage('#page_1', {reverse: false, changeHash: false});
//your code here...
}
This should fit your needs, just try it out. "#page_loading" would be your login page, "page_1" your list page...
Keep in mind that changeHash:false refers to the destination page, not to the source. You will not be removing the source page from history. Instead, the history hash will not be updated when moving to the new page
If you use the latest version of jQuery mobile (1.4+) you can use this script:
$.mobile.pageContainer.pagecontainer('change', '#page', {reverse: false, changeHash: false});
jQuery.mobile.changePage is deprecated as of jQuery Mobile 1.4.0 and will be removed in 1.5.0.
Adding options for reverse and changeHash did not work for me. using Cordova v1.6
I ended up overriding the onTouch method in my Android Activity.
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_BACK == keyCode) {
// Clear browsers history if user clicks back button
clearHistory();
}
return super.onKeyUp(keyCode, event);
}
Related
I have multiple pages in my one html file.
I trying to implement the pageinit event handler on the second data-role="page".
So I declared pageinit inside it's specific data-role="page".
<div data-role="page" id="foo3" data-dom-cache="false">
<script>
$(document).on('pageinit','#foo3' , function(){
abcsong_file_path = '/android_asset/www/audio/abcsong.mp3';
my_abc = new Media(abcsong_file_path);
my_abc.play();
var i =0;
var time;
function my_loop(){
setTimeout(function (){
var my_alphabets = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
$('#content_loop2').append('<img src="img/alphabets/'+my_alphabets[i]+'.png" />');
i++;
time = 700;
if(i<26)
{
my_loop();
}
}, time)
}
my_loop();
});
</script>
<div data-role="header" data-theme="b">
</div>
<div data-role="content" >
<div id="content_loop2" data-inset="true">
</div>
</div>
<div data-role="footer" >
</div>
</div>
What I expected was that it would initialize everytime I visit this page. But it runs correctly only the first time I open it. Every other time it just show the output of previously executed code.
Please help me how to go about it.
Pageinit should run only once, it was made to be just like document ready.
If you want your code to run every time page is visited then use pageshow or pagebeforeshow.
Read more about it here.
Pageinit should fire only once, according to the docs, but at least in previous versions that was not the actual truth.
I am currently using jQM 1.3.2 and no longer experiencing this problem on Android or in desktop browser. Pay attention to it though, especially if you are also using Phonegap.
Document pageinit fires more than once on iOS (jQueryMobile)
I have a main navbar, which loads contents - using .load() - to a div data-role=content according to whats selected,
so i use the TapHandler event on the navbar, to load the content like:
$("#mainc").load(target, function() {
$("#index").trigger("pagecreate"); //#index is the page id
});
Div where content is being loaded:
<div data-role="content" id="mainc">
</div>
Whats being loaded: ( the second navbar )
<div id="profile_navbar" data-role="navbar">
<ul>
<li>My Profile</li>
<li>Events</li>
<li>Settings</li>
</ul>
</div>
And the problem is, the first click, evrything goes perfect, then, it starts duplicating some borders on and on, the more i change tabs, the more borders it gain,
i dont use .trigger('create') there, because it doesnt get the loaded content styled with the jqm css.
Images:
What is going on here?
Thanks.
After some hours of struggle, I found that the problem was not with .trigger(“pagecreate”) but with .load()
So i came up with this: (dont ask me why, all I know is that it works this way)
$.get(target, function(data) {
$('#mainc').html(data);
$('#mainc').trigger("create");
});
instead of:
$("#mainc").load(target, function() {
$('#mainc').trigger("create");
});
I am currently exploring jQuery Mobile's functionalities. I am quite intrigued by the way Google Play Store handles horizontal sliding, hence, when the user slide taps to the right, the view should slide to the next page and when the user slide taps to the left the view should scroll to the previous page, if any. I know this can be done using native jQuery but I'm not yet familiar with events on mobile devices and I'm sure there is already a built-in functionality for this.
I'd like to try this first with Android devices and if possible with iPad and iPhone. Can anybody guide me on ways to accomplish this?
BTW, I'm not talking about browser history here, probably just some div, pages, if possible.
It can be done but you will be sadly disappointed.
It can be achieved like this:
Multiple page's inside one single HTML. Every page will have swipeleft and swipe right binded to it. When event is triggered changePage() function will make a transition to previous/next page. This sounds excellent and works just fine on desktop browser but fails miserable when executed with phonegap on android phones. Transitions are still a huge problem on android phones, iOS fares better but not to much.
Something like this:
$('#page-two').on('#page-two', 'swipeleft', function () {
//next page
$.mobile.changePage($('#page-three'));
}).on('#page-two', 'swiperight', function () {
//prev page
$.mobile.changePage($('#page-one'), { reverse : true });
});
Swipe events are supported with jQuery Mobile so no need for 3rd party plugins.
Use a jQuery Mobile carousel plugin like this example: http://jsfiddle.net/blackdynamo/yxhzU/
Original plugin: https://github.com/blackdynamo/jQuery-Mobile-Carousel
Unlike page transitions this plugin will give you much better feeling on mobile phones.
What ever path you choose android tab look will be achieved with navbar inside a second header:
<div data-theme="a" data-role="header">
<h3>
First Page
</h3>
Next
</div>
<div data-theme="a" data-role="header">
<div data-role="navbar">
<ul>
<li>Page One</li>
<li>Page Two</li>
<li>Page Three</li>
</ul>
</div><!-- /navbar -->
</div>
Solution #1 -- This should be the easy way to do it:
<script>
$(document).delegate("#homepage", 'pageinit', function (evt) {
$(this).bind("swipeleft", function (e) {
$.mobile.changePage("#anotherpage", {
transition : 'slide'
});
});
}).delegate("#anotherpage", 'pageinit', function (evt) {
$(this).bind("swiperight", function (e) {
$.mobile.changePage("#homepage", {
transition : 'slide',
reverse : true
});
});
});
</script>
Solution #2 -- This one is even much simpler:
$('#homepage').bind('swipeleft', function() {
$.mobile.changePage('#anotherpage', {transition: 'slide', reverse: false});
});
$('#anotherpage').bind('swiperight', function() {
$.mobile.changePage('#homepage', {transition: 'slide', reverse: true});
});
I`m trying to accomplish more than one addEventListener, but something is wrong?
For example if we have 3 divs on page and first one is displayed on the beginning and other two hidden.
<div id="d1">
<a onClick="
document.addEventListener("backbutton", show_div1, false);
$('#d1').hide();
$('#d2').show();
"
</a>
</div>
<div id="d2"></div> - initially hidden
<div id="d3"></div> - initially hidden
It shows div 2, hides div 1 and sets listener for back button to show_div1(), and everything works OK. On back key pressed it alerts "I should show #div1", as it should (//$('#d1').show(); is comented)
show_div1(){
//$('#d1').show(); $('#d2').hide();
alert ('I should show #div1');
}
But now comes the problem
<div id="d2">
<a onClick="
document.removeEventListener("backbutton", show_div1, false);
document.addEventListener("backbutton", show_div2, false);
$('#d2').hide();
$('#d3').show();
"
</a>
</div>
It immediately fires "I should show #div2" even back button is not pressed! addEventListener like started main function show_div2() and not just set listener on back button to that function.
show_div2(){
//$('#d2').show(); $('#d3').hide();
alert ('I should show #div2');
}
What could be the possible reason for this happening?
try this,
on device ready add a listener for backbutton like this
var onBackButton = function(){show_div2();}; //the initial state
document.addEventListener("backbutton", onBackButton, false);
And
don't use onClick with phoneGap
use href, or ontouchend and be sure that your < a > is visible because you dont have anything inside(display block in your css file)
<div id="d1">
<a href='javascript:onDivClick(1)' style='display:block; width:100%; height:100%;'></a>
// <a ontouchend='onDivClick(1)'></a> will be better
</div>
<div id="d2">
<a href='javascript:onDivClick(2)' style='display:block; width:100%; height:100%;'></a>
</div>
in javascript
function onDivClick(var case)
{
switch(case)
case 1:
$('#d1').hide();
$('#d2').show();
onBackButton = function(){show_div1();};
break;
case 2:
$('#d2').hide();
$('#d1').show();
onBackButton = function(){show_div2();};
break;
}
I am creating an application using JQuery Mobile which needs to support both iOS and Android. (I am using PhoneGap). Does scrolling on long pages work by default or do I need to set up my divs to support scrolling. How is scrolling on iOS different from an android device?
Also, is there a way to make a ajax call when the user scrolls to the bottom of the page to retrieve more content?
Any help is appreciated.
1) jQuery Mobile 1.1.0 uses the browser's native scrolling so it seems natural on every supported platform.
jQuery Mobiles does however require the following pseudo-page structure:
<div data-role="page">
<div data-role="header">
...
</div>
<div data-role="content">
...
</div>
<div data-role="footer">
...
</div>
</div>
If you follow this structure, the more you add to the data-role="content" section, the longer the page will be.
2) You can set an event handler for the scroll event to detect the user scrolling and see how far down the page the user is:
//you don't want to set a bunch of AJAX requests at once,
//so set a flag so only one will go off at a time
var ajaxOK = true;
$(window).on('scroll', function () {
var yDistance = $('html, body').scrollTop();
//here you can check how far down the user is and do your AJAX call
if ((yDistance + $(window).height()) > ($.mobile.activePage.children('.ui-content').height() - 150)) {
//the user is within 150px of the bottom of the page
if (ajaxOK === true) {
ajaxOK = false;
$.ajax({ ... });
}
}
});
Then in your callback function for the AJAX call you set ajaxOK = true; so that when the user scrolls to the bottom again it will fire.
Here's a quick break-down of the if/then statement in the scroll event handler:
(yDistance + $(window).height()) > ($.mobile.activePage.children('.ui-content').height() - 150)
(yDistance + $(window).height()): the scroll-distance plus the view-port height, to find the bottom of the page's Y coordinate.
($.mobile.activePage.children('.ui-content').height() - 150): the height of the current page minus a buffer of 150px so the user can get within 150px and the AJAX call will occur
Scrolling should happen automatically if you overflow the browser window. For infinite scrolling you can try http://www.infinite-scroll.com/.
If you are using the listview for jquery mobile you may need to call the refresh event after adding more list view items to the dom to get the filtering behavior and styling to work.