JQM json request won't parse on mobile - javascript

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.

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">

Apache Cordova doesn't load all page div

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.

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>

alert message not displayed in jquery mobile application

I am new to jquery and jquery mobile. I am currently trying to create a simple mobile application that, once a page is loaded, displays an alert message.
I have added in the head section of the html file, as suggested, the following code:
<link rel="stylesheet" href="jquery.mobile-1.3.0.min.css" />
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$('#chart').live('pageinit', function() {
alert('jQuery Mobile: pageinit for Config page');
});
</script>
<script type="text/javascript" src="jquery.mobile-1.3.0.min.js"></script>
In the body section I have added, as expected, the code for the chart page:
<div data-role="page" data-theme="b" id="chart">
<div data-role="header" data-position="fixed">
Back
<h1>Year Chart</h1>
</div>
<div data-role="content">
<div id="container"></div>
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
</div>
</div>
</div>
However, when the chart page is loaded in the browser of my computer (not the phone), no alert message is displayed. Does anyone know where the problem may lie? Thanks in advance!
Here is the full HTML and code that I can success alert in my all browser(IE, chrome, firefox). I guess some of your javascript are not exist or typo. Try to use CDN.
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
<script type="text/javascript">
$( '#chart' ).live( 'pageinit',function(event){
alert( 'This page was just enhanced by jQuery Mobile!' );
});
</script>
</head>
<body>
<div data-role="page" data-theme="b" id="chart">
<div data-role="header" data-position="fixed">
Back
<h1>Year Chart</h1>
</div>
<div data-role="content">
<div id="container"></div>
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
</div>
</div>
</div>
</body>
</html>
UPDATED:
If you are using jquery 1.9+, we should use .on instead of .live. It is because .live has been removed from 1.9. http://api.jquery.com/live/
So, the code should change like this if you are using 1.9
$( document ).on( 'pageinit','#chart', function(event){
alert( 'This page was just enhanced by jQuery Mobile!' );
});
I think that jQuery Mobile page events (like pageInit) don't bubble outside the page. Try to insert the script tag inside the the page div like this
<div data-role="page" data-theme="b" id="chart">
<div data-role="header" data-position="fixed">
Back
<h1>Year Chart</h1>
</div>
<div data-role="content">
<div id="container"></div>
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
</div>
</div>
<script type="text/javascript">
$('#chart').live('pageinit', function() {
alert('jQuery Mobile: pageinit for Config page');
});
</script>
</div>
Alternatively you can bind the pageInit function to the document element like this:
$(document).live('pageinit', function() {
alert('jQuery Mobile: pageinit for Config page');
});
But I think the first solution is more inline with the best practices

jquerymobile session timeout with 2 timers

Edit: I see the issue is with using live instead of on. I fixed that however, can't get the second timer to fire..
Error: Object [object Object] has no method "popup"
I am trying to implement a client side session time out using jquery mobile. My code is at: http://jsfiddle.net/83BSW/5
Appreciate any insights..
Here is the code for convenience:
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
<!-- Application specific -->
<script>
var first_timer = 2 * 1000;
var second_timer = 3 * 1000;
var down = -1;
function initTimer() {
down = setTimeout(processTimeout, first_timer)
}
function processTimeout() {
down = setTimeout(logout, second_timer);
$.mobile.changePage('#timeout1',{transition:'slide', role:'dialog'});
//alert ("Timeout of first timer, timerid:" + down );
}
function logout() {
$("#timeout").popup('close');
$.mobile.changePage('#timeout2',{transition:'slide', role:'dialog'});
alert("You are logged out");
// window.location = "http://www.google.com"
}
function resetMyTimer() {
if ( -1 != down )
clearTimeout(down);
alert("Restarting timer");
initTimer();
}
$("#loadingpage").on(function() {
resetMyTimer();
});
initTimer();
</script>
</head>
<body>
<div data-role="page" id="loadingpage">
<div data-role="header" data-position="fixed">
<div class="headerlogo"> </div>
<h1> Test </h1>
</div>
<div data-role="content" >
<div id="ssagov">
<h1> Hi there </h1>
</div>
<input type="button" data-shadow="false" data-corners="false" value="Next" />
</div><!-- /content -->
</div><!-- /launch page -->
<div data-role="page" id="timeout1" data-role="popup">
<div data-role="header" data-backbtn="false">
<h1>Timeout1</h1>
</div>
<div data-role="content">
Your session will timeout in 2 mins.
</div>
</div>
<div data-role="page" id="timeout2" data-role="popup">
<div data-role="header" data-backbtn="false">
<h1>Timeout2</h1>
</div>
<div data-role="content">
Your session has timed out
</div>
</div>
</body>
</html>​
Finally I fixed the issue, instead of popup, I should be using dialog. Here is the working version: http://jsfiddle.net/83BSW/6

Categories

Resources