JSfiddle code won't work in phonegap

29 views
Skip to first unread message

Kerry Fang

unread,
Sep 17, 2016, 10:55:47 PM9/17/16
to phonegap
Someone kindly helped me with storing data into local storage by showing me a sample jsfiddle code: https://jsfiddle.net/adeneo/2uod274d/
However when I try to run the code in my phonegap (using Xcode, building app for iOS), the code doesn't work at all. Anyone have any ideas why? I've included         <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.min.js"></script> but still doesn't work.

Diego La Monica

unread,
Sep 18, 2016, 5:33:57 AM9/18/16
to phon...@googlegroups.com
Hi Kerry,
  1. Connect your device to your mac, 
  2. enable web inspector and 
  3. check if your code gives you some error.



2016-09-18 4:55 GMT+02:00 Kerry Fang <fangy...@gmail.com>:
Someone kindly helped me with storing data into local storage by showing me a sample jsfiddle code: https://jsfiddle.net/adeneo/2uod274d/
However when I try to run the code in my phonegap (using Xcode, building app for iOS), the code doesn't work at all. Anyone have any ideas why? I've included         <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.min.js"></script> but still doesn't work.

--
-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com
---
You received this message because you are subscribed to the Google Groups "phonegap" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phonegap+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
--
Diego La Monica
Web Solution Developer
Software, Web, Standard, Accessibility
IWA Member #0279702
W3C Protocols and Formats Working Group member for IWA/HWG
Web Skill Profiles WG Member (http://skillprofiles.eu )

Email: m...@diegolamonica.info
Skype: diego.la.monica
Twitter: @jast
Mobile: +393337235382
Web: http://diegolamonica.info

Kerry Fang

unread,
Sep 18, 2016, 6:37:28 AM9/18/16
to phonegap
There is indeed an error when I click on "Save Data": JSON Parse error: Unexpected identifier "Sat"
pointing to the line:
var dates = data ? JSON.parse(data) : [];


On Sunday, September 18, 2016 at 7:33:57 PM UTC+10, Diego La Monica wrote:
Hi Kerry,
  1. Connect your device to your mac, 
  2. enable web inspector and 
  3. check if your code gives you some error.


2016-09-18 4:55 GMT+02:00 Kerry Fang <fangy...@gmail.com>:
Someone kindly helped me with storing data into local storage by showing me a sample jsfiddle code: https://jsfiddle.net/adeneo/2uod274d/
However when I try to run the code in my phonegap (using Xcode, building app for iOS), the code doesn't work at all. Anyone have any ideas why? I've included         <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.min.js"></script> but still doesn't work.

--
-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com
---
You received this message because you are subscribed to the Google Groups "phonegap" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phonegap+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Kerry Fang

unread,
Sep 18, 2016, 6:45:25 AM9/18/16
to phonegap

And here's my full code (slightly modified compared to the ones in jsfiddle to match my app)

JS:
 <script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
<script type="text/javascript">
document
.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady() {
document
.getElementById("btnSave").addEventListener("click",saveData,false);
document
.getElementById("btnShow").addEventListener("click",showData,false);
}
function saveData(){
var data = window.localStorage.getItem("date");
var dates = data ? JSON.parse(data) : [];

dates
.push( new Date() );
window
.localStorage.setItem("date", JSON.stringify(dates));
alert
("Your data is stored");
}

function showData() {
var data = JSON.parse(window.localStorage.getItem("date"));
console
.log(data);
$
('#res').html(JSON.stringify(data));
$
(this).html('Update result');
}
</script>


HTML:

 <button id="btnSave"> Save Data </button>
<button id="btnShow"> Show Data </button>

One thing I forgot to mention is that the app works if I just put a simple local storage setItem code, this code will store the date into local storage when the button is pressed, however this code will overwrite the date which isn't something I want the app to do:
function saveData()

{ var date= new Date();

window
.localStorage.setItem("date", JSON.stringify(date));
alert
("Your data is stored");
}

On Sunday, September 18, 2016 at 7:33:57 PM UTC+10, Diego La Monica wrote:
Hi Kerry,
  1. Connect your device to your mac, 
  2. enable web inspector and 
  3. check if your code gives you some error.


2016-09-18 4:55 GMT+02:00 Kerry Fang <fangy...@gmail.com>:
Someone kindly helped me with storing data into local storage by showing me a sample jsfiddle code: https://jsfiddle.net/adeneo/2uod274d/
However when I try to run the code in my phonegap (using Xcode, building app for iOS), the code doesn't work at all. Anyone have any ideas why? I've included         <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.min.js"></script> but still doesn't work.

--
-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com
---
You received this message because you are subscribed to the Google Groups "phonegap" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phonegap+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Kerry Fang

unread,
Sep 18, 2016, 10:52:29 AM9/18/16
to phonegap
I run the app again, and for some reason the previous error is gone when I click "Save Data", and this time the error changed to: TypeError: dates.push is not a function. (In 'dates.push( new Date() )', 'dates.push' is undefined)


On Sunday, September 18, 2016 at 7:33:57 PM UTC+10, Diego La Monica wrote:
Hi Kerry,
  1. Connect your device to your mac, 
  2. enable web inspector and 
  3. check if your code gives you some error.


2016-09-18 4:55 GMT+02:00 Kerry Fang <fangy...@gmail.com>:
Someone kindly helped me with storing data into local storage by showing me a sample jsfiddle code: https://jsfiddle.net/adeneo/2uod274d/
However when I try to run the code in my phonegap (using Xcode, building app for iOS), the code doesn't work at all. Anyone have any ideas why? I've included         <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.min.js"></script> but still doesn't work.

--
-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com
---
You received this message because you are subscribed to the Google Groups "phonegap" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phonegap+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Diego La Monica

unread,
Sep 18, 2016, 11:07:35 AM9/18/16
to phon...@googlegroups.com
Maybe the dates object is not an array but something else.
Set a breakpoint there and check again.

Good luck!



For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com
---
You received this message because you are subscribed to the Google Groups "phonegap" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phonegap+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages