Apache Cordova doesn't load all page div - javascript

I'm developing an app with PhoneGap and I want to create a simple between some pages.
This is my html file
<html>
<head>
<title>Prova</title>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
</head>
<body>
<div class="prima" data-role="page" id="article1">
<div data-role="header" data-theme="b" data-position="fixed" data-id="footer">
<h1>Articles</h1>
</div>
<div data-role="content">
<p>Article 1</p>
</div>
<div data-role="footer" data-theme="b" data-position="fixed" data-id="footer">
<h1>Footer</h1>
</div>
</div>
<div class="prima" data-role="page" id="article2">
<div data-role="header" data-theme="b" data-position="fixed" data-id="footer">
Home
<h1>Articles</h1>
</div>
<div data-role="content">
<p>Article 2</p>
</div>
<div data-role="footer" data-theme="b" data-position="fixed" data-id="footer">
<h1>Footer</h1>
</div>
</div>
<div class="prima" data-role="page" id="article3">
<div data-role="header" data-theme="b" data-position="fixed" data-id="footer">
Home
<h1>Articles</h1>
</div>
<div data-role="content">
<p>Article 3</p>
</div>
<div data-role="footer" data-theme="b" data-position="fixed" data-id="footer">
<h1>Footer</h1>
</div>
</div>
<link rel="stylesheet" href="css/jquery.mobile-1.4.5.css" />
<script src="js/jquery.js"></script>
<script src="js/jquery_mobile.js"></script>
<script src="js/gianni.js"></script>
</body>
</html>
My js file is (http://jsfiddle.net/Gajotres/NV6Py/)
$(document).on('swipeleft', '.prima', function(event){
if(event.handled !== true) // This will prevent event triggering more then once
{
var nextpage = $(this).next('.prima');
console.log(nextpage);
// swipe using id of next page if exists
if (nextpage.length > 0) {
$.mobile.changePage(nextpage, {transition: "slide", reverse: false}, true, true);
}
event.handled = true;
}
return false;
});
$(document).on('swiperight', '.prima', function(event){
if(event.handled !== true) // This will prevent event triggering more then once
{
var prevpage = $(this).prev('.prima');
console.log(prevpage);
if (prevpage.length > 0) {
$.mobile.changePage(prevpage, {transition: "slide", reverse: true}, true, true);
}
event.handled = true;
}
return false;
});
When I open this html file it works and read all page div but if I use this page in my app where I have some pages before arrive to this page the swipe doesn't work because var nextpage = $(this).next('.prima') is empy and if I check with Ispector in html there is only the fist div and no other (ex. article2, article3)

Your question is a bit unclear, but unfortunately I can't comment on your question because of my low reputation. So I can't ask you for more clarification.
But you have 3 divs all with the class: prima. And your problem is that you can only see the first div, so the div with the id: article1.
The reason why you can only see one div, is because you use the data-role: page. You are creating a page, and you can only see one page at the time.

Related

JavaScript not working for sliding the pages

I am following the jsfiddle. The code sample is working fine on jsfiddle, However I am unable to make it to work in my cordova mobile app.
I have placed the javascript code in home.js in js folder.
home.js:
$(document).on('swipeleft', '.ui-page', function(event){
if(event.handled !== true) // This will prevent event triggering more then once
{
var nextpage = $.mobile.activePage.next('[data-role="page"]');
// swipe using id of next page if exists
if (nextpage.length > 0) {
$.mobile.changePage(nextpage, {transition: "slide", reverse: false}, true, true);
}
event.handled = true;
}
return false;
});
$(document).on('swiperight', '.ui-page', function(event){
if(event.handled !== true) // This will prevent event triggering more then once
{
var prevpage = $(this).prev('[data-role="page"]');
if (prevpage.length > 0) {
$.mobile.changePage(prevpage, {transition: "slide", reverse: true}, true, true);
}
event.handled = true;
}
return false;
});
home.html:
<!DOCTYPE html>
<html>
<head>
<title>Share QR</title>
<meta name="viewport" content="width=device-width,height=device-height,minimum-scale=1,maximum-scale=1"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" />
<!--<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>-->
<script type="text/javascript" src="js/home.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
</head>
<body>
<div data-role="page" id="article1">
<div data-role="header" data-theme="b" data-position="fixed" data-id="footer">
<h1>Articles</h1>
</div>
<div data-role="content">
<p>Article 1</p>
</div>
</div>
<div data-role="page" id="article2">
<div data-role="header" data-theme="b" data-position="fixed" data-id="footer">
Home
<h1>Articles</h1>
</div>
<div data-role="content">
<p>Article 2</p>
</div>
</div>
<div data-role="page" id="article3">
<div data-role="header" data-theme="b" data-position="fixed" data-id="footer">
Home
<h1>Articles</h1>
</div>
<div data-role="content">
<p>Article 3</p>
</div>
</div>
</body>
</html>
I can't swipe to change the pages. Could someone point me in right direction ?
Seems you are missing the 'ui-page' class from your HTML.
<div class="ui-page" data-role="page" id="article1">

jquery show hide the requested page

Problem is to show/hide requested specific page from among pages in the html. The code I tried is shown below. Can any one point me the right direction. I n the blow code I tried to show only pagethree, but it displayed pageone.
<!DOCTYPE html PUBLIC >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="jquery.mobile-1.2.0.min.css" />
<script src="jquery-1.8.1.min.js"></script>
<script src="jquery.mobile-1.2.0.min.js"></script>
<script>
$("#pageone").hide();
$("#pagetwo").hide();
$("#pagethree").show();
</script>
</head>
<body>
<div data-role="page" id="pageone">
<div data-role="header">
<h1>page one</h1>
</div>
<div data-role="main" class="ui-content">
<p>page one contents </p>
</div>
<div data-role="footer">
<h1>page one</h1>
</div>
</div>
<div data-role="page" id="pagetwo">
<div data-role="header">
<h1>page two</h1>
</div>
<div data-role="main" class="ui-content">
<p>page two contents</p>
</div>
<div data-role="footer">
<h1>page two</h1>
</div>
</div>
<div data-role="page" id="pagethree">
<div data-role="header">
<h1>page three</h1>
</div>
<div data-role="main" class="ui-content">
<p>page three contents...</p>
</div>
<div data-role="footer">
<h1>page three</h1>
</div>
</div>
</body>
</html>
To prevent page from showing or redirect a user before next page is shown, you need to listen to pagebeforechange event. This event fires before changing page transition is commenced and URL History is updated.
When pagebeforechange is emitted, it omits a data object holding details of previous and next page. Use this data to determine from and which pages the user is navigating.
$(document).on("pagebeforechange", function (e, data) {
var nextPage = data.toPage,
prevPage = data.options.fromPage;
if (typeof nextPage === "object" && typeof prevPage === "undefined") {
var page = nextPage[0].id;
if (page === "pageone") {
$.mobile.changePage("#pagethree", {
transition: "flip"
});
return false;
}
}
});
Using .show() or .hide() will get you nowhere. In addition, refrain from using .ready() or $(function () {}); in jQuery Mobile and use Page Events instead.
Demo
Try to put your code inside pagecreate event:
Triggered when the page has been created in the DOM (via ajax or
other) and after all widgets have had an opportunity to enhance the
contained markup.
<script>
$(document).on('pagecreate', function() {
$("#pageone").hide();
$("#pagetwo").hide();
$("#pagethree").show();
});
</script>
<script>
$(document).ready(function()
{
$("#pageone").hide();
$("#pagetwo").hide();
$("#pagethree").show();
});
</script>

swapping two pages and have a static one

I have three pages A,B,C and i want to swap right from A to B then go back, and swap left from A to C then go back ...
I'm new to jquery mobile and javascript, I've tried a lot of scripts but non of them did the job,
here is my js and html
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="jquery.mobile-1.4.1.min.css">
<script src="jquery-1.10.2.min.js"></script>
<script src="jquery.mobile-1.4.1.min.js"></script>
<script src="jquery-1.9.1.min.js"></script>
<script src="jquery.mobile-1.3.1.min.js"></script>
</head>
<body>
<div data-role="page" id="page1" >
<div data-role="header">
<h1>page1</h1>
</div>
<div data-role="main" class="ui-content">
<p>page1</p>
</div>
<div data-role="footer">
<h1>swap</h1>
</div>
</div>
<div data-role="page" id="page2">
<div data-role="header">
<h1>page2</h1>
</div>
<div data-role="main" class="ui-content">
<p>page2</p>
</div>
<div data-role="footer">
<h1>swap</h1>
</div>
</div>
<div data-role="page" id="page3">
<div data-role="header">
<h1>page3</h1>
</div>
<div data-role="main" class="ui-content">
<p>page3</p>
</div>
<div data-role="footer">
<h1>swap</h1>
</div>
</div>
</body>
</html>
js
<script>
$(document).on('swiperight swipeleft', function(event) {
if (event.type == 'swiperight') {
if(id == 'page1'){
$.mobile.changePage("#page3");
}
else
{
$.mobile.changePage("#page1");
}
}
if (event.type == 'swipeleft') {
if(id == 'page1'){
$.mobile.changePage("#page2");
}
else
{
$.mobile.changePage("#page1");
}
}
});
</script>
Add in page header of #page2 and #page3
<a data-rel="back" href="#" data-transition="slide" direction="reverse">Back</a>
Javascript
$(document).on('swiperight swipeleft', "#page1", function(event) {
if (event.type == 'swiperight') {
$.mobile.changePage("#page2");
}
if (event.type == 'swipeleft') {
$.mobile.changePage("#page3");
}
});

JQM json request won't parse on mobile

I'm developing a mobile app that uses json to retrieve database data. Using my mac browser I can get the requested data and the alert and display it on a new mobile page. I can't get it to work on safari on ipad or iPhone. The page loads but no data.
Any hints would be appreciated.
HTML code
<!DOCTYPE HTML>
<html>
<head>
<title>tourmap_main.jpg</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script src="js/treedetails14.js"></script><div data-role="page" id="firstpage" data-theme="a">
<div data-role="header" data-position="fixed">
<h1>Winter Hill Garden Tour</h1>
</div>
<div data-role="content" id="mainpage" data-theme="a">
<div id="area1Button">AREA 1
</div>
</div>
<div data-role="footer" data-theme="a" data-position="fixed"></div>
</div>
<div data-role="page" id="area1" data-add-back-btn="true" data-theme="a">
<div data-role="header" data-position="fixed">
<h1>Garden Tour - Area 1</h1>
</div>
<div id="area1content" data-role="content">
<div id="tree105" class="treebutton">Button 1
</div>
<div id="tree106" class="treebutton">Button 2
</div>
</div>
</div>
<div data-role="page" id="detailsPage" data-overlay-theme="a">
<div data-role="header" data-add-back-btn="true">
<h1 align="left"><span>Tree information -</span> <span id="treetitle"></span></h1>
</div>
<div id="treeDetails" data-role="content">
<div id="treedescription"></div>
</div>
<div data-role="footer" data-theme="a" data-position="fixed"></div>
</div>
And my script:
$(document).on('pagebeforeshow', '#firstpage', function(){
$('.treelink').live('click', function(){
var serviceURL = "http://winterhill.com.au/gardentour/services/";
currentId = $(this).attr('data-id');
$.getJSON(serviceURL + 'gettree.php?id='+currentId, function(data) {
var tree = data.item;
alert(tree.tree_description);
$('#detailsPage').append("<p>item1="+tree.tree_description +"</p>");
});
});
});
Here's the basic code setup:
http://jsfiddle.net/VgxnQ/1/
Try this code:
$('#area1').on('pagebeforeshow', function () {
$('.treelink').on('click', function () {
currentId = $(this).attr("data-id");
window.sessionStorage.setItem("IdKey", currentId);
$.mobile.changePage( "#detailsPage",
{ reverse: false,
changeHash: false
});
});
});
$('#detailsPage').on('pagebeforeshow', function () {
currentId = window.sessionStorage.getItem("IdKey");
var serviceURL = "http://winterhill.com.au/gardentour/services/";
$.getJSON(serviceURL + 'gettree.php?id=' + currentId, function (data) {
var tree = data.item;
$('#treedescription').text(tree.tree_description);
});
});
Still there is Access allow origin issue that needs to be resolved at the server level either have it JSONP enabled or enable the setting that it allows any domain that access the web service.

Swiping in jQuery and Phonegap works unexpectedly

I'm using the code given in this answer to swipe several pages in my Phonegap application.
However, it seems that the live function is deprecated and furthermore, a "0" appears below the page when I try to reload. As I try more, a string of zeroes is created, the number of each telling the number of reloads of that page.
Long story short: swiping works but a 0 appears in each load of the swiped page.
I tried to change to this but doesn't seem to work (I'm using Phonegap 2.1.0, jQuery 1.8.2 and jQuery Mobile 1.1.1).
<script type="text/javascript">
$('div.ui-page').on("swipeleft", function () {
var nextpage = $(this).next('div[data-role="page"]');
if (nextpage.length > 0) {
$.mobile.changePage(nextpage, {
transition: "slide",
reverse: false
}, true, true);
}
});
$('div.ui-page').on("swiperight", function () {
var prevpage = $(this).prev('div[data-role="page"]');
if (prevpage.length > 0) {
$.mobile.changePage(prevpage, {
transition: "slide",
reverse: true
}, true, true);
}
});
</script>
Edit: I tried with this and the same as the initial problem happens:
<script type="text/javascript">
$(document).delegate('div.ui-page', 'swipeleft', function () {
var nextpage = $(this).nexy('div[data-role="page"]');
if (nextpage.length > 0) {
$.mobile.changePage(nextpage, {
transition: "slide",
reverse: false
}, true, true);
}
});
$(document).delegate('div.ui-page', 'swiperight', function () {
var prevpage = $(this).prev('div[data-role="page"]');
if (prevpage.length > 0) {
$.mobile.changePage(prevpage, {
transition: "slide",
reverse: false
}, true, true);
}
});
</script>
That version of on is equivalent to bind. To replace live you need
$(document).on("swipeleft", "div.ui-page", function...
To further debug the event, check what $(this) refers to - it may not be the page you think it is, and may not have a next / previous element.
It depends a certain amount on how your app is set up, but in general you can't rely on the page divs existing in any particular order.
Also, the call to changePage doesn't appear to match the current documentation (
http://jquerymobile.com/test/docs/api/methods.html ) - what are the two trues at the end?
I don't know if answering my own question is of good etiquetter, but i found the solution and I think it'd be handy being visible.
I updated to jQuery 1.8.2 and jQuery mobile 1.2.0 and everything worked. An example that works is here:
<!DOCTYPE HTML>
<html>
<head>
<title>EventApp</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/css/jquery.mobile.structure-1.2.0.min.css" />
<script type="text/javascript" charset="utf-8" src="js/core/cordova-2.1.0.js"></script>
<script type="text/javascript" charset="utf-8" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.js"></script>
<script type="text/javascript">
$(document).ready( function() {
$('div.ui-page').live("swipeleft", function () {
var nextpage = $(this).next('div[data-role="page"]');
if (nextpage.length > 0) {
$.mobile.changePage(nextpage, {
transition: "slide",
reverse: false
});
}
});
$('div.ui-page').live("swiperight", function () {
var prevpage = $(this).prev('div[data-role="page"]');
if (prevpage.length > 0) {
$.mobile.changePage(prevpage, {
transition: "slide",
reverse: true
});
}
});
});
</script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h2 class="ui-title">Page one</h2>
</div>
<div data-role="content">
You are in page one.
</div>
<div data-role="footer" data-id="foo1" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Home</li>
<li>Info</li>
<li>Settings</li>
</ul>
</div><!-- /navbar -->
</div><!-- /footer -->
</div>
<div data-role="page">
<div data-role="header">
<h2 class="ui-title">Page two</h2>
</div>
<div data-role="content">
You are in page two.
</div>
<div data-role="footer" data-id="foo1" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Home</li>
<li>Info</li>
<li>Settings</li>
</ul>
</div><!-- /navbar -->
</div><!-- /footer -->
</div>
<div data-role="page">
<div data-role="header">
<h2 class="ui-title">Page three</h2>
</div>
<div data-role="content">
You are in page three.
</div>
<div data-role="footer" data-id="foo1" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Home</li>
<li>Info</li>
<li>Settings</li>
</ul>
</div><!-- /navbar -->
</div><!-- /footer -->
</div>
<div data-role="page">
<div data-role="header">
<h2 class="ui-title">The map</h2>
</div>
<div data-role="content">
<div id="map_canvas"></div>
</div>
<div data-role="footer" data-id="foo1" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Home</li>
<li>Info</li>
<li>Settings</li>
</ul>
</div><!-- /navbar -->
</div><!-- /footer -->
</div>
</body>
And if you want the JsFiddle is here

Categories

Resources