Converting a dynamic web site to a PhoneGap application

This post is more than 2 years old.

Earlier today a reader asked me about the possibility of converting his mobile-friendly site into a "real" application via PhoneGap. I told him that this could be very easy. You can take your HTML, upload it to the PhoneGap Builder service, and see what you get. This works with simple HTML sites, but is not going to work well with dynamic sites built with server-side languages. In this blog post, I'll explain why it won't work, and also walk you through an example of converting a (simple) dynamic web site into a PhoneGap application.

Before I begin - two quick notes. I'll be using jQuery Mobile for the UI and ColdFusion for the back-end. This is completely inconsequential to the task at hand. Ok, ready?

First, let's discuss why a dynamic web site can't be simply converted as is into a PhoneGap application. Most of my readers are web developers so you should know this, but when it comes to dynamic server-side languages like ColdFusion, PHP, Ruby, etc, the HTML your browser gets is created dynamically.

Take for example this simple ColdFusion site. When you request this URL with your browser, my web server hands off the request to ColdFusion. ColdFusion does its magic (hits the database) and outputs raw HTML. That HTML is returned to the browser and rendered as is. The same would apply for PHP, Ruby, etc. When you click on the detail page, we hit one template that is passed a URL parameter that instructs the code to load a particular record and display it.

Now - consider PhoneGap. PhoneGap takes your HTML files and packages them up into a native application for your mobile platform. But it is not a web server. You can't bundle in ColdFusion or PHP and have it execute server-side code like in the example above.

Does this mean you're completely out of luck? Not at all. Let's look at how we can convert our code into a PhoneGap application.

First, let's look at the initial application. As I said above, the choice of the server-side language isn't relevant to the discussion. Therefore, I won't go into detail about what the ColdFusion code is doing. Those of you who don't know ColdFusion should be able to mentally map it to the language of your choice. First - the index page.

Then the detail page:

And finally - here is the component that drives the data. Basically it just wraps up the logic to get our list and detail.

Ok - so that's our old school (although nicely mobile optimized) web site. To begin the conversion to PhoneGap, I create a new file for my home page that is pure HTML, no ColdFusion.

Notice that the layout is the same as before, but our content is gone. Previously that was sourced on the server by a database call. So how do we add this dynamic data back in? With JavaScript.

First - let's add some logic to run when the home page is created. This specific event is based on how jQuery Mobile does things, but again, you could do this without any particular UI framework.

This code block performs a HTTP request to our server. (Note: I'm using localhost in the example above but in a real application it would be your site's domain, something.com.) I've built a new set of server-side code just to handle getting and returning data in JSON format. So there's still a server involved, but now it's simply returning data, nothing more. I loop over the result and render it out into the page.

The detail page is built much like the index page. It's a copy of the earlier version minus any code or actual content.

To handle this, back in my JavaScript I added code to run when the page is loaded.

And that - as they say - is basically it. To summarize:

  • The code in the PhoneGap application is just HTML and JavaScript.
  • The dynamic data from the earlier application was rewritten to expose itself remotely.
  • PhoneGap then simply uses Ajax to fetch that data.

I've attached a zip of all the code used for this blog post below. If any part of this doesn't make sense, let me know, and I hope this was helpful.

Download attached file.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Terry Jett posted on 5/5/2012 at 9:09 PM

Thank you Raymond, excellent tutorial.

I have not yet tried PhoneGap but been wanting to. Your tutorial has peaked my interest.

Can one use PHP files with PhoneGap, or does it have to be HTML files? Like to use some includes for a project in mind.

Thanks again Raymond, great share.

Terry

Comment 2 by Don posted on 5/5/2012 at 9:29 PM

Has to be HTML files... PhoneGap can't understand any server-side languages (PHP, ColdFusion, ASP, etc)

Comment 3 by Raymond Camden posted on 5/5/2012 at 9:30 PM

"Can one use PHP files with PhoneGap, or does it have to be HTML files?"

Well, shoot, that was kind of the major point. :) If I didn't make that clear, I apologize. So no - you cannot. It has to be HTML.

To be anal, you can use .txt, .xml, .csv, if you want to use JavaScript to parse them. The point is - you can't use a platform like PHP/CF within the PhoneGap app itself. Rather, you would use it via a network call.

Clearer?

Comment 4 by Mikel posted on 5/6/2012 at 5:30 AM

This is such a handy example! Thanks a lot for taking the time to put this together. I'm sure that many will find it very useful.

I'm giving a small PhoneGap presentation at work this Friday and I already have several of your pages bookmark to share with the group. Well, now I have to add this one too :)

Comment 5 by Don Vawter posted on 5/6/2012 at 9:04 PM

I don't believe this example will fly on a device because it relies on localhost to serve the page and there will be no localhost cf server on the device. One would have to reference a remote cfserver. That is easily done. The only gotcha is that you have to remember to whitelist the url in phonegap. As a side note you don't have to worry about cross domain ajax calls because the built in server on the device acts as a proxy for the requests.

As a general practice I have moved away from using cf in my presentation pages even on desktop applications and use cf only as a data server. - Conversion to mobile becomes much simpler and imo javascript with jQuery is much better at presentation.

Comment 6 by Raymond Camden posted on 5/6/2012 at 9:14 PM

The localhost thing was just a demonstration. I used a full URL on purpose - and obviously (ok, maybe it wasn't ;) the host would change depending on your server. Should I add a note for that point above?

Comment 7 by Don Vawter posted on 5/6/2012 at 9:38 PM

@Ray. Might be a good idea. Once you understand how PhoneGap works it is obvious you can't use localhost but if one is just learning it may not be, especially since localhost may very well work in a simulator.

Comment 8 by Raymond Camden posted on 5/6/2012 at 10:14 PM

Added a small note. Let me know if you think it helps.

Comment 9 by Don Vawter posted on 5/6/2012 at 11:40 PM

@Ray I think the note will help. Appreciate your responsiveness.

Comment 10 by Terry Jett posted on 5/6/2012 at 11:49 PM

Sorry to ask such a stupid question. Should have been clear that HTML could not be used. Last time I will visit using a tablet that does not show code correctly.

I will refrain from such in the future.

Comment 11 by Raymond Camden posted on 5/6/2012 at 11:51 PM

Terry, I hope I didn't sound too hard in my response. If so, I apologize. I have a fix for showing code in the mobile version, but I've yet to deploy it to my server. I'm going to do so Monday.

Comment 12 by Raymond Camden posted on 5/6/2012 at 11:57 PM

Terry (et all) - I felt bad about this - so I pushed the fix up. For folks curious, it is based on this blog entry:

http://www.raymondcamden.co...

Comment 13 by Brian Swartzfager posted on 5/7/2012 at 6:37 PM

It seems like this is a situation where a Javascript templating library like Handlebars would come in handy: it would simply be a matter of including the templating library in the PhoneGap project, wouldn't it (or are there issues with doing that)?

Comment 14 by Raymond Camden posted on 5/7/2012 at 6:41 PM

Brian - I'd say it depends. I specifically didn't go deep into the JS because the blog post was more about the _conversion_. The topic of how to actually build it will be depend on your JS skills, what libraries you like, etc.

That being said - I freaking love Handlebars - and yes - it works on mobile. :)

Comment 15 by Patrick Whittingham posted on 5/7/2012 at 7:29 PM

Raymond,

I think you can use 'server-side' code in a PhoneGap application for both Android and Apple. This example will point to code on a web server

Here is an example for Android.

package com.test.test8t;

import com.phonegap.*;
import android.os.Bundle;

public class test8T_srvActivity extends DroidGap {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setBooleanProperty("loadInWebView", true);
super.setIntegerProperty("loadUrlTimeoutValue", 80000);
super.loadUrl(
"https://www.test_server/apps/folder1/www/index.html");
}
}

Comment 16 by Raymond Camden posted on 5/7/2012 at 7:37 PM

I see that as a solution, but most people I know doing PhoneGap do not do it that way. Also, this would have zero support for offline mode. Admittedly, so would using Ajax to fetch the data, but you could at least handle it better, switch to a local db for backup data, etc. But yea - that is an option.

Comment 17 by Mark Gregory posted on 5/8/2012 at 4:36 AM

Forgive the noobish question, but do Phone Gap apps run in Webkit, or do they get compiled to native code. I am at the starting line here, and considering Phone Gap versus Titanium (http://www.appcelerator.com/), which is a sort of metered service it appears.

Comment 18 by Raymond Camden posted on 5/8/2012 at 5:10 AM

It runs in a webkit browser run via native code. It's like a thin wrapper of deliciousness around the browser.

Comment 19 by Mark Gregory posted on 5/8/2012 at 5:14 AM

Ahh I see! Seeing as it runs from webkit, does it give access to hardware goodies like cameras, GPS, acceleromator, etc?

(Pretty sure I spelled that accelerator part wrong!)

Comment 20 by Raymond Camden posted on 5/8/2012 at 5:19 AM

Absolutely - check the docs for an example. Here's a recorded Intro preso: http://www.raymondcamden.co...

Also, tomorrow, Andrew Trice and I are doing an open preso you can attend.

Comment 21 by Mark Gregory posted on 5/8/2012 at 8:26 AM

Ah Crikey! What am I waiting for?!?!
Thank you sir!

Comment 22 by Ali posted on 6/3/2012 at 8:32 PM

Thank you. What would be your approach if it the site is a little more complicated. Maybe with login and logout, and user based views etc.

Comment 23 by Raymond Camden posted on 6/3/2012 at 9:54 PM

There's a couple things to consider here.

1) Login is certainly possible. Its just one more Ajax call. If your back end supports cookie-based sessions, then your PG app's Ajax calls will pass those cookies back in future calls. That means your back end code could ensure a session exists (and has a verified login) before returning any data.

2) User based views: Your login method could return a hashmap of allowed views, or roles, that your client app would use to render stuff. Obviously your back end code still needs to do validation, based on the session info.

Comment 24 by Kulbhushan posted on 6/18/2012 at 5:34 PM

Hello Ray,

We are in the process of creating a PhoneGap application and the functionality would also be accessible from the mobile browser.

We are having a discussion as to which option to use:
1) Send json from server and keep html pages in the phonegap app.
2) Send hrml pages populated with data to the phonegap app.

The advantages we see are:
1) Fixing bugs is easier, it is server side instead of getting the app certified all over again
2) Probably more re use of code.

Do you have any comments on this?

Comment 25 by Raymond Camden posted on 6/19/2012 at 4:59 AM

When you mention the advantages, I'm not sure which option you are speaking of. To me, you get more reuse if your server-side application is already using MVC. Your PG app can hit the model via a service layer and reuse the same business logic.

Comment 26 by Phil Baumert posted on 6/28/2012 at 8:18 PM

Hi, thanks for this super useful blog! I have this working on my application but I just want to have a different index page with a button to link to a page(i call it "intList.html") with the code you have on this examples index.html. For some reason, it does not load the data unless it is on the index.html page. What am I missing here? Thanks for your help. This is my index.html that has a link to intList.html with code from your index.html.

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>RYP</title>
<link rel="stylesheet" href="css/jquery.mobile-1.1.0.min.css" />
<script src="js/jquery-1.7.2.min.js"></script>
<script src="js/jquery.mobile-1.1.0.min.js"></script>
<script src="js/main.js"></script>
</head>

<body>

<div data-role="page" id="indexPage">

<div data-role="header">
<h1>Shows</h1>
<img src="css/images/ryprad_logo.jpg"></h1>
</div>

<br><br><br>

<div data-role="content">
<a href="intList.html">Shows</a><br><br>
</div>
<br><br><br>
<div data-role="footer">

<h4>Stat</h4>
</div>

</div>
</body>
</html>

Comment 27 by Phil Baumert posted on 6/29/2012 at 12:01 AM

Hey there again. I figured out everything runs "through" the index.html file. It works now. Thanks again for this awesome example!

Comment 28 by Shawn Puyear posted on 8/10/2012 at 6:36 PM

Please forgive the noob question, but when I preview the app in a browser, v1 displays just fine but v2 just displays a page with header and footer and no content. I changed the service call to point to my local server. Did I forget to do something?

Comment 29 by Raymond Camden posted on 8/10/2012 at 6:45 PM

v2 depends on "deviceready" being fired by PhoneGap. You can't just run it in the browser. You could - if you want - run it and open your console and run startup() manually.

Comment 30 by Shawn Puyear posted on 8/10/2012 at 7:02 PM

Thank you. If I wanted to view it working in a browser, is there a different event to call?

Comment 31 by Raymond Camden posted on 8/10/2012 at 7:16 PM

It depends on your code. If your code says, when deviceready is fired, run X, then you want to run X.

Comment 32 by Shawn Puyear posted on 8/23/2012 at 10:17 PM

Ray. Thanks again for your help. I got the data to display in a browser but cannot, for the life of me, get it to display in the Android. I even tried adding a button on the page to call startup() with no luck in the emulator.

Could you suggest a method to fire the event to call and return the data in the emulator?

Comment 33 by Raymond Camden posted on 8/24/2012 at 12:39 AM

Did you remember to include cordova.js?

Comment 34 by joseph posted on 9/6/2012 at 2:29 PM

Hi Raymond,

I created a web site in PHP and I load it in a phone gap app using onload function and ajax. Now I want to call phonegap function from my code in server. For example I want to take a photo can I call that photo function in my app ?

Comment 35 by Raymond Camden posted on 9/6/2012 at 4:55 PM

What do you mean you 'load' it? Are you loading just data or something else?

Comment 36 by Shawn Puyear posted on 9/18/2012 at 5:28 PM

Ray: Sorry for the long post. I am still hitting a snag and hope you can help. I did include a version of cordova.js in my site. Below is my head tag set.

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Art Lister</title>
<link rel="stylesheet" href="css/jquery.mobile-1.1.0.min.css" />
<script src="js/jquery-1.7.2.min.js"></script>
<script src="js/jquery.mobile-1.1.0.min.js"></script>
<script src="cordova-2.0.0.0.js"></script>
<script src="js/main.js"></script>

</head>

To run you through my process, I did the following:
1. In Dreamweaver, I created a new site
2. I copied the files from your v2 folder into the new site
3. I copied the artservice.cfc to my testing server and made sure it was accessible
4. I change the cfc call in the index,html page to pint to the cfc on my testing server
5. I built the app using the PhoneGap functionality built into Dreamweaver
6. I launched the app from Dreamweaver in an Android Virtual Device that I previously created

The app installs fine in the AVD and opens, just no data is displayed. Only the header and footer show.

Any ideas why the data is not displayed? Thanks!

Comment 37 by Raymond Camden posted on 9/18/2012 at 5:43 PM

The browser (and PhoneGap) on your AVD cannot hit localhost - which I assume you are using. Well, it can, but localhost is local to the device, not your hardware. Try using 10.0.0.2 instead, which should point to your local machine.

Comment 38 by Shawn Puyear posted on 9/18/2012 at 6:17 PM

I wish it were that simple. I actually have the cfc on an external test server that I access via IP.

Comment 39 by Raymond Camden posted on 9/18/2012 at 6:19 PM

Oh, well then it should just work. I'd suggest checking the console. http://www.raymondcamden.co...

Comment 40 by Shawn Puyear posted on 9/18/2012 at 6:35 PM

That is what I figured. Thanks again for your help.

Comment 41 by Rikesh Subedi posted on 10/10/2012 at 8:46 PM

Hi Raymond,
I'm new to coldfusion and I want to work on PhoneGap. I loaded all your files in a local host and tried to run V1/index.cfm but is shows an error "Datasource cfartgallery could not be found." May i know the reason please?

Comment 42 by Raymond Camden posted on 10/11/2012 at 1:15 AM

That's a sample database you can get when installing ColdFusion.

Comment 43 by Rikesh Subedi posted on 10/15/2012 at 8:38 AM

XMLHttpRequest cannot load http://localhost:8500/service/artservice.cfc?method=getArtList&returnformat=json. Origin null is not allowed by Access-Control-Allow-Origin.

how to overcome this error

Comment 44 by Raymond Camden posted on 10/15/2012 at 11:18 AM

Are you using a browser or a PhoneGap app to test?

Comment 45 by al posted on 11/3/2012 at 2:28 AM

As part of ajax call, can phonegap get HTML with scripts to download and execute on the mobile device?

Comment 46 by Raymond Camden posted on 11/3/2012 at 5:24 PM

Written correctly, yes. Imagine your remote HTML/script has a block of code that looks like this:

function foo() { alert('x'); }

This cannot be added to the app when loaded in via Ajax. Instead, your code must be written as

foo = function() { alert('x'); }

Comment 47 by max rossi posted on 11/8/2012 at 4:33 AM

hi raymond,
I tried this example these days, I installed the android sdk, everything ok, but my cfc works if I call the method with returntype="string", but doesn't work with returntype="query". Unfortunately I have no errors from the debug. I call the cfc in the external server with cf8....help help help please....thank you very much

Comment 48 by Raymond Camden posted on 11/8/2012 at 6:05 PM

I'd need more details. So for example, you say it doesn't work, but exactly how? In your code that handles the response, if you alert() the response, do you see anything? Also, have you tried jQuery's Ajax error handling?

Comment 49 by max rossi posted on 11/8/2012 at 10:37 PM

My response is alert(undefined)
my cfc:

<cffunction name="getfatture" access="remote" returntype="query" output="false">

<cfquery datasource="#request.datasource#" name="sel_fatture">
SELECT CT_Fatture.id_fattura, CT_Clienti.Societa
FROM CT_Fatture
INNER JOIN CT_Clienti ON CT_Fatture.id_cliente = CT_Clienti.id_cliente
</cfquery>

<cfreturn sel_fatture>
</cffunction>

and script:

<script>
$("#indexPage").live("pageinit", function() {
console.log("Getting remote list");
$.mobile.showPageLoadingMsg();

$.get("http://invoice.contechnet.i...", {}, function(res) {
$(document).ready(function(){
alert(res.length);
});
$.mobile.hidePageLoadingMsg();

var s = "";
for(var i=0; i<res.length; i++) {
s+= "<li><a href='detail.html?id=" + res[i].id_fattura + "'>" + res[i].societa + "</a></li>";
}

$("#artList").html(s);
$("#artList").listview("refresh");
},"json");
});
</script>

thank you

Comment 50 by Raymond Camden posted on 11/8/2012 at 10:45 PM

Woah - why do you have a document.ready block _inside_ your get response? That makes no sense and won't work at all. Remove that and keep the alert line.

Comment 51 by max rossi posted on 11/9/2012 at 3:08 AM

ok, but I get the same answer: undefined

Comment 52 by Raymond Camden posted on 11/9/2012 at 3:36 AM

Don't alert res.length, just res.

Comment 53 by max rossi posted on 11/9/2012 at 4:55 AM

yeah, you're right, sorry, alert[object Object]

if i use
alert(JSON.stringify(res, null, 4));

i get

{"COLUMNS":["ID_FATTURA","SOCIETA"],"DATA":[[2,"Receptour Srl"],[3,"Contech Sas"],[4,"Receptour Srl"],[5,"Receptour Srl"],[6,"Zama World Visa"]]}

thank you very very much

Comment 54 by Raymond Camden posted on 11/9/2012 at 7:52 AM

Ok, so as you can see, the structure of your data is different. You can't do .length on it. You have an object with 2 main properties, COLUMNS and DATA. COLUMNS is an array of column names. DATA is an array of arrays where each element is one row of data, and each element in THAT array maps to the same COLUMNS array.

Comment 55 by John Hartman posted on 1/10/2013 at 12:34 AM

Raymond, great post and extremely helpful. Question. My mobile site is JQM and codeigniter php. The entire site is php based from a file naming convention as well as functions within. What I'm trying to wrap my head around is do I need to create the "template" html files as you have done for each page that needs to be pulled, or can I simply create an index.html page that does an ajax call to index.php? currently, all views are generated back thru the index.php page. hope that makes sense.

regards,

John

Comment 56 by Raymond Camden posted on 1/11/2013 at 1:06 AM

@John: I'm afraid I don't understand your question. I've described this technique as generically as I can. It should be applicable to any dynamic language. I don't quite know what your codeigniter does so I can't really comment.

Comment 57 by Marc Schipperheyn posted on 2/14/2013 at 9:37 PM

For much more complicated server side templates, retrieving json and recreating the display logic on the mobile device can be a real pain. Stuff like Handlebars is extremely limited, especially when you are used to powerful server side scripting languages such as Cold Fusion or Freemarker.

I was thinking more along the lines of retrieving the actual HTML in stead of json content and inserting that dynamically. There are some issues to overcome at first glance:
* you would have to create some generic way of handling form submissions that integrates nicely with your existing server side infrastructure.
* returned templates shouldn't contain any script src references.
* you need to deal with session timeouts gracefully
* you would have to do all this and at the same time avoid the Apple AppStore police saying it's too "webby"

Non of this seems major. Any thoughts on this?

Comment 58 by Raymond Camden posted on 2/15/2013 at 4:43 PM

I attended a session by a Twitter dev last year where he talked about doing much the same thing. They found that creating HTML on the client (from data) was a bit too slow for them so they moved more to generating the HTML server side. So yes - I don't think this is crazy at all and it may work better for you.

Comment 59 by Zana posted on 4/25/2013 at 2:09 AM

Hi Raymond. I have a question. I'm using phonegap to create a web app and when i want to browse dynamically to another page as you did, it doesn't work and what i get is that the page is not found. Example : bloc_adresse ='<li><a href="page.html" rel="external"> '+titre+'</a></li>'; works but
var bloc_adresse ='<li><a href="page.html?id='+id+' "rel="external"> '+titre+'</a></li>'; doesn't work. Can you please tell me if you have any idea.

Comment 60 by Raymond Camden posted on 4/25/2013 at 2:14 AM

There was a bug w/ urls with query string in older Androids/PhoneGap. Are you running the latest PhoneGap on a somewhat newish Android?

Comment 61 by Zana posted on 4/25/2013 at 2:58 AM

I'm running CordovaWP7 2.4.0 i'm doing the app for windows phone.

Comment 62 by Zana posted on 4/25/2013 at 3:01 AM

I'm downloading the 2.6.0 to test. will tell you if it works.

Comment 63 by Raymond Camden posted on 4/25/2013 at 5:31 PM

Wait, why are you doing rel=external? You don't need/want that.

Comment 64 by Kalgi posted on 5/9/2013 at 2:08 PM

Hi Ray,
One question is this .cfm will work in iphone too ??
actually i have tried & the problem i got is, below code is written in index.html
<body>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta HTTP-EQUIV="REFRESH" content="5; url=index.cfm">
</body>
when i run my app i m getting an error like :
Failed to load webpage with error: Frame load interrupted..
Can you please help me ??

Comment 65 by Raymond Camden posted on 5/10/2013 at 6:06 PM

Kalgi, your question does not make any sense. My post isn't about making a CFM work on a mobile device. It is about how you would rewrite, or extend, an existing server-side app so you can use it from your mobile device with HTML and JS.

Comment 66 by Jerry posted on 6/27/2013 at 7:08 PM

I am a newbie to PhoneGap.
Typically PhoneGap app start from a local HTML. How could the try to get other domain work?
$.get("http://anotherdomain/..."...)

Comment 67 by Raymond Camden posted on 6/27/2013 at 9:57 PM

You can't do that. Well technically you can - but Apple won't approve an app that just loads up the entire content from your site.

Comment 68 by Rupesh Kumar posted on 7/22/2013 at 9:26 AM

hey Raymond,

with the help of your example (and few extra source). i'm using the following jquery mobile code to get json data
$.ajax({
type: "POST",
url: "http://projects.corewebconn...",
cache: false,
dataType: "json",
data: formData,
success: onSuccess,
error: onError
});

function onSuccess(data, status)
{
var output = $('#projectlist');
var place = '';

output.empty();
$.each(data, function(i,value){
place += '<li><a href="">' + value.firstName + '<span class="ui-li-count">' + value.id+'</span></a></li>';

});
$("#projectlist").html(place);
$("#projectlist").listview("refresh");

//console.log(data);
}

my html has following code
<div id="main-content" class="content" data-role="content">
<ul id="projectlist" data-role="listview" data-filter="true" data-filter-placeholder="Search projects..." data-inset="true">

</ul>
</div>

i can make it run on browsers (chrome, safari, FF), but not on emulator or device. i have been struggling for 2 days. i'm using dreamweaver cs6 and phonegap.

Comment 69 by Raymond Camden posted on 7/22/2013 at 3:43 PM

You didn't say how it failed on the emulator/device. If you are using iOS, I recommend remote debugging with Safari to see if you can expose the error better.

Comment 70 by rupesh kumar posted on 7/22/2013 at 4:23 PM

please check this url: http://www.qhse-riskapplica... (you can view console (on chrome) to see json output.

but when i run this on emulator (uploaded the screenshot on http://www.qhse-riskapplica..., it just shows the search box and no list view.

i have changed the code since my last point

updated jquery code:
<link rel="stylesheet" href="http://code.jquery.com/mobi..." />
<script src="http://code.jquery.com/jque..."></script>
<script src="http://code.jquery.com/mobi..."></script>

<script>
$(document).on('pagebeforeshow', '#home', function(){

$.getJSON("http://www.qhse-riskapplica...", function(projects) {
$("#projectlist").empty();

$.each(projects, function(i, projects){
$("#projectlist").append(generateProjectLink(projects));
console.log(generateProjectLink(projects));
//alert(projects.firstName);
});

$("#projectlist").listview("refresh");

});

function generateProjectLink(projects){
return '<li><a href="">' + projects.firstName + '<span class="ui-li-count">' + projects.id+'</span></a></li>'
}
});
</script>

my html has following code
<div id="main-content" class="content" data-role="content">
<ul id="projectlist" data-role="listview" data-filter="true" data-filter-placeholder="Search projects..." data-inset="true">

</ul>
</div>

i'm using dreamweaver cs6 and phonegap.

i'm stuck big time :-(

Comment 71 by Raymond Camden posted on 7/22/2013 at 5:42 PM

Oh, you probably need to addprojects.corewebconnecti... to your ACCESS element. Please see the PhoneGap docs on this.

http://docs.phonegap.com/en...

Comment 72 by Rupesh Kumar posted on 7/23/2013 at 9:18 AM

thanks for your reply

i added <access origin="http://www.qhse-riskapplica..." /> to config.xml. is there any specific place to store it. i have this file in http://www.qhse-riskapplica.... the config file looks like bellow

?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = "com.phonegap.example"
version = "1.0.0">

<name>Audit</name>
<access origin="http://www.qhse-riskapplica..." />

<description>
Audit and Compliance
</description>

<author href="https://www.domain.com" email="you@domain.com">
Rupesh Kumar
</author>

</widget>

but adding this also did not work. i hope i get a working example as per my json
http://www.qhse-riskapplica...

Comment 73 by Raymond Camden posted on 7/23/2013 at 3:45 PM

I replied via email as well, but I think your issue is the access value. Try changing origin to qhse-riskapplications.com.

Comment 74 by Rupesh Kumar posted on 7/24/2013 at 2:39 PM

thanks Raymond for your help. i have moved forward. but now i ran into another problem

i have index.html file with following changePage function

$.mobile.changePage( "show_projects.html", { transition: "slideup", changeHash: false });

the problem is that when i run "show_projects.html" directly, it loads the data from json (perfect).

but when i call index.html first and then use $.mobile.changePage( "show_projects.html", { transition: "slideup", changeHash: false }); "show_projects.html" does not load json, any idea? the jquery code on show_projects.html is

$(document).on('pageinit', '#navigation', function(){ $.ajax({url: "http://projects.domain.com/...", dataType: "json", jsonpCallback: 'successCallback', async: true, beforeSend: function() { $.mobile.showPageLoadingMsg(true); }, complete: function() { $.mobile.hidePageLoadingMsg(); }, success: onSuccess, error: function (request,error) { alert('Network error has occurred please try again!'); } }); });

Comment 75 by Raymond Camden posted on 7/24/2013 at 8:25 PM

Is #navigation the page id for show_projects.html?

Comment 76 by Mark posted on 8/9/2014 at 4:29 AM

HI Raymond, very informative site. I'm trying to convert an application running on Coldfusion MX 7.0.2 into a mobile app. First thing I need to do I guess is be able to access my CF server code using javascript which brought me here.

I can't get this example working at all. Just get a page with the headings, no data. I created a directory called mobile on my server account and uploaded the three directories (V1, V2, service) in the zip file. I ran the code <my domain>mobile/v1/index.html and just get the headings. If I look using firebug the cfc is generating a 500 server error. If I go directly to the cfc file in the browser with <my domain>mobile/service/artservice.cfc?method=getArtList I get the following error:

"The method 'getArtList' could not be found in component WEB-INF.cftags.component. Check to ensure that the method is defined, and that it is spelled correctly."

Any idea what's going wrong. Also is there a super simple example of accessing and displaying a CFQUERY from javascript. I figure if I can at least get that working it's a good first step. I've tried about every jquery, ajax sample on the web and I can't get any of them working. Some I can get the cfc working directly from the browser but then can't get the data to display in the HTML page. Very frustrating, any help appreciated.

Comment 77 by Raymond Camden posted on 8/9/2014 at 4:41 AM

Does the CFC have a method called getArtList?

Comment 78 by Mark posted on 8/9/2014 at 5:52 AM

No, I guess not. I picked up the example from browsing the web so I thought that the function name would equate to method, since.

test.cfc
<cffunction name="get_all_users" access="remote" hint="Gets all users in the database" >
could be accessed from the browser by /test.cfc?method=get_all_users

that
artservice.cfc
remote array function getArtList() {
could be accessed from the browser by /artservice.cfc?method=getArtList

Apparently not, so not sure why the call from the index.html javascript in your example :
http://localhost/testingzone/phonegaptests/conversion/service/artservice.cfc?method=getArtList&returnformat=json
and in my example
http://www.mydomain.com/mob...

is generating a 500 server error and how I can test the cfc directly.

Comment 79 by Raymond Camden posted on 8/9/2014 at 6:15 AM

Um, having some trouble parsing your comment. So... if the file is called artservice.cfc and the method in there is getallusers, then yes, you can hit it at your domain.com/ANY PATH HERE/artservice.cfc?method=getallusers.

Maybe share your real URL so we can hit it and see.

Comment 80 by Adrian posted on 9/3/2014 at 7:13 AM

Hi Raymond!

Thx for this interesting idea.

I have a question: Is there any way for a webpage to call a PhoneGap app?
I want to upload images taken with the device's (tablet, phone) camera.

Have you heard about any way to do that?

Have a great day!

Comment 81 by Raymond Camden posted on 9/3/2014 at 3:13 PM

The closest I can think of would be intents with Android. This article seems to cover the iOS way: http://iosdevelopertips.com...

Give those a shot first.

Comment 82 by Anakin posted on 11/24/2014 at 8:50 AM

Is there a recommended way to store the external urls we hit through the Phonegap app. Do we use the config.xml for it ? I tried Google but didnt get any specific answers

Comment 83 (In reply to #82) by Raymond Camden posted on 11/24/2014 at 12:47 PM

Sorry, what external URLs?

Comment 84 by Abha posted on 1/3/2015 at 7:32 AM

i am new in phonegap.
I can't import this in my eclipse ... what can i do .......???

Comment 85 (In reply to #84) by Raymond Camden posted on 1/3/2015 at 5:01 PM

You do not need to use Eclipse to do PhoneGap. It sounds like you need some more basic education about PG - I'd suggest the docs or picking up my book (still in early access): http://www.manning.com/camden/

Comment 86 by abey kp posted on 1/9/2016 at 10:38 AM

Hi Raymond, Thank you for the excellent tutorial. I was wondering if I can package just the landing index.html (lets make it a login page) page along with all css and js files into phonegap app. from there on, once the user logs in can we fetch data directly from the other files located on webserver and display inside the app?

How do I go about doing this?

Comment 87 (In reply to #86) by Raymond Camden posted on 1/9/2016 at 7:09 PM

This is exactly what my post is talking about. Or am I misreading your question?

Comment 88 by markus posted on 1/24/2016 at 5:58 AM

Hi Raymond,

I just learnt about phonegap. Does it mean it will be far more easier to convert to phonegap if i have the existing web app that mostly written in front end js framework like knockout js or angular js? And is phonegap app identical to single page application?

Thank you your article really helps me understanding the phonegap.

-Markus

Comment 89 (In reply to #88) by Raymond Camden posted on 1/25/2016 at 7:56 PM

"Does it mean it will be far more easier to convert to phonegap if i have the existing web app that mostly written in front end js framework like knockout js or angular js?" Yes - that would help.

"And is phonegap app identical to single page application?" Not necessarily. PhoneGap/Cordova describes how you convert your HTML/JS/CSS to a mobile app. It does NOT tell you how to write your code. So a PhoneGap/Cordova app *may* be a SPA or not. It is certainly recommended, but not required.

Comment 90 by Petros Labropoulos posted on 8/14/2016 at 11:16 PM

hi Raymond,

I am an electrical engineer and my thesis for my msc is about a smart home using arduino and all of this to be handled by an android smartphone. I have written the code which is actually an html including xml-ajax-javascript. The only problem is that i don't know how to convert all of those an android app. I can send you my code if you are willing to help.

Thanks in advance,

-Petros

Comment 91 (In reply to #90) by Raymond Camden posted on 8/15/2016 at 1:15 PM

It sounds like you are asking how to build a hybrid app in general. If so, I'd go to http://cordova.apache.org and start learning about Apache Cordova. You can also buy my book - it is on sale from my About page.

Comment 92 by Yelitza CintrĂ³n posted on 8/25/2016 at 4:40 PM

Hi!! :) Can I use this to call my wordpress page? So I can controlled the content in my CMS without having to write anything in phone-gap, just this script to call the web?

Also, is this can be used as the page warp, I can add the Push Notification Page to it? For example, I send a push, and they all be on an app tabs, where I can see all notifications listed? Or PG just let you send notifications and you can't see a list of notifications you have received on your device on this particular app?

Comment 93 (In reply to #92) by Raymond Camden posted on 8/25/2016 at 5:47 PM

You can point to an external site, but if your phone goes offline than nothing will work.

I'm sorry - but your second paragraph makes no sense to me.

Comment 94 by adda ghalamallah posted on 11/24/2016 at 11:52 AM

Hi,
Is it possible to convert a JQuery + PHP + Mysql web site to an application without minimum rework ?
Tkx !

Comment 95 (In reply to #94) by Raymond Camden posted on 11/24/2016 at 4:45 PM

Honestly it depends on your app.

Comment 96 by Ridlo Putuismaya posted on 4/20/2017 at 4:19 AM

can I build if I just use index.php ? not using index.html

Comment 97 (In reply to #96) by Raymond Camden posted on 4/20/2017 at 12:24 PM

I'm not sure what you mean. In this article, I'm discussing how to convert a dynamic site to one for PG - the technology is ColdFusion, but that is secondary to the general guidelines. It would apply to Node, PHP, .Net, etc. If you're asking if you can follow the same guide for a PHP site, well then sure, and if anything made you think you couldn't, let me know and I'll clear it up in the article.

Comment 98 by IwingMusic posted on 5/15/2017 at 5:52 PM

i have a wordpress self hosted website, can you write tutorial for it?

Comment 99 (In reply to #98) by Raymond Camden posted on 5/15/2017 at 5:54 PM

Not really. I explained the process in general here. It should get you started in the right direction.

Comment 100 (In reply to #99) by IwingMusic posted on 5/16/2017 at 1:01 PM

ok thx. i just found a wp plugin for phonegap called appkit. i successfully built my app. https://uploads.disquscdn.c...

Comment 101 (In reply to #100) by Raymond Camden posted on 5/16/2017 at 1:58 PM

Grats. :)

Comment 102 by Manny posted on 5/24/2017 at 1:57 PM

Download attachment link broke!

Comment 103 (In reply to #102) by Raymond Camden posted on 5/25/2017 at 1:01 PM

Link fixed.

Comment 104 by Manny posted on 5/25/2017 at 2:50 PM

Ray

Thanks for the link fix.

Just now starting to work with the phonegap. I have tried to play with your code but am stumped. I feel that its a brain cramp but the data from the cfc is returned as

{"COLUMNS":["FIELD_MST_MLS_NUMBER"],"DATA":[[95938],[101048],[115971],[109493],[109860]]}

I can see the columns console.log(res.COLUMNS.length) ; returns 1
console.log(res.COLUMNS) ; returns ["FIELD_MST_MLS_NUMBER"]
For the life of me I cannot get to the DATA.
Manny

Comment 105 (In reply to #104) by Raymond Camden posted on 5/25/2017 at 4:09 PM

It is under res, so res.DATA. The result is an array of arrays, where the item in [0][0] represents the first row, and first col, which for you is FIELD_MST_MLS_NUMBER.

Comment 106 (In reply to #105) by nottakingthisanymore posted on 8/17/2017 at 4:23 PM

I did not give you credit back a few months ago. Thanks that work for me!!
Manny

Comment 107 (In reply to #106) by Raymond Camden posted on 8/17/2017 at 4:26 PM

No worries - glad it helped.

Comment 108 by Jimmy Medrano posted on 10/13/2017 at 11:29 PM

Ray,

I have a web application built in C#, implementing ASP.NET. The front-end consists of .NET Razor (.cshtml extension) instead of regular HTML and javascript. I am curious as to how to convert such an application to mobile with PhoneGap. Will I need to completely overhaul the Razor Views into plain HTML and then re-write the logic with javascript? I appreciate your article and any clarification you may provide.

Thank you,

Jim

Comment 109 (In reply to #108) by Raymond Camden posted on 10/14/2017 at 2:43 PM

Without knowing what Razor Views are I'd have to say yes. At the end, they have to present "normal" HTML/JS to the browser, so *that* is what you have to work with, not Razor.