PhoneGap Javascript not working other than in index file - javascript

Hi I'm trying to run a javascript function (an alert to test) but it is not firing when in a page other than the index.html.
here is an example of a page that is not firing the javascript alert ( note that < is removed from all of the tags so as to render in stack overflow):
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
//
function onDeviceReady() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
alert("test");
navigator.notification.alert("PhoneGap is working");
}
</script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.css" />
<link rel="stylesheet" href="docs/_assets/css/jqm-docs.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="experiments/themeswitcher/jquery.mobile.themeswitcher.js"></script>
<script src="docs/_assets/js/jqm-docs.js"></script>
<script src="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script>
<link rel="stylesheet" href="n_style.css" />
</head>
<body onload="onBodyLoad()">
</body>
</html>

onDeviceReady only fires once when the app starts up. See documentation:
http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html#deviceready
It's the function that indicates PhoneGap is fully loaded.

Your Javascript isn't running because you don't have an onBodyLoad function. I would wrap document.addEventListener("deviceready", onDeviceReady, false); in an onBodyLoad function. Also, if you are having javascript errors, try opening the page in firefox and using firebug to check for errors.

Related

Why doesn't my console.log message show when I press Start?

I'm learning to use event listeners, I wanted to log pressing the "Start" button to the console to make sure the button works but I'm not getting any results when I test the .html file in devtools.
I've verified the index.js name referenced in the .html file is the same
I've tried the source code in the header
I've added the src=index.js jQuery library through Google to the bottom before </body>
I've copied the code to repl.it to try a different environment
JAVASCRIPT:
function startQuiz() {
$('#startButton').click(function(event){
console.log("Keep Going");
});
}
HTML:
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Herbal Medicine: Adaptogens</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link rel="questions" href="store.html">
</head>
<body>
<div class="container">
<p>Intro to Herbal Adaptogens explaining what they are</p>
<button id="startButton">Start</button>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="index.js"></script>
</body>
</html>
I expect the to see "Keep Going" in the console when I press "start" but instead nothing happens at all.
You have the JQuery click() function inside the startQuiz() function. So it won't work unless the outer function (startQuiz()) is run first. You should put it inside a ready function so that it loads once the page is 'ready'.
Change your javascript to look like this:
$(document).ready(function() {
$('#startButton').click(function(event) {
console.log("Keep Going");
});
});
No need to go with jQuery , you can just use js events.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Log Window</title>
</head>
<body>
<script>
function logMe() {console.log('clicked');}
</script>
<button onclick="logMe()">logMe</button>
</body>
</html>
You should put it inside a Jquery click function. This way it will fire when the document loads.
$(document).ready(function() {
$('#startButton').click(function(event) {
console.log("Keep Going");
}
});

Ajax will not load when FTP is loaded

I'm just trying to get the hang of using Ajax with FTP so it'll load my changes in real-time. However, not matter how I change the code it just won't do what i want it to and to my knowledge the code itself seems fine...All it's suppose to do is alert the contents of my test.html file ("this is some content") when I load it into the ftp.
Learning JQuery
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script type="text/javascript" src="jquery.min.js">
</script>
<link rel="stylesheet" type="text/css" href="CSS/jquery.css">
</head>
<body>
<h1>Asyncronous Javascript And XML </h1>
<script type="text/javascript" src="JS/index.js"></script>
</body>
</html>
Ajax:
$.get("test.html", function(data) {
alert(data);
});

Only one time welcome screen on android

i am making an html5/javascript application at Intel XDK and i would like to add a welcome screen which will be displayed only once ever.
any help is welcome.
thanks in advance!
You can use Cordova localStorage to save a value to detect first time or not.
Below is working sample:
<!DOCTYPE html>
<html>
<head>
<title>App</title>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0;" />
</head>
<body>
<h1>Hello World</h1>
<script src="cordova.js"></script>
<script>
function onDeviceReady() {
welcomeScreen();
}
document.addEventListener("deviceready", onDeviceReady, false);
function welcomeScreen(){
var welcome = window.localStorage.getItem("welcome");
if(!welcome){
window.localStorage.setItem("welcome", "1");
alert("Welcome Message"); // replace with welcome screen display
}
}
</script>
</body>
</html>

Uncaught TypeError: Cannot read property 'PhotoSwipe' of undefined in javascript

I'm trying to implement swiping of image using html and javascript for phonegap application, following this demo PhotoSwipe there it works fine, but after adding into my project, not able to swipe the images. and even linked the js files correctly still i get this error in logcat "Uncaught TypeError: Cannot read property 'PhotoSwipe' of undefined at file:///android_asset/www/examples/01-default.html:26"
any idea on my issue? Thanks.
swipe code
<title>PhotoSwipe</title>
<meta name="author" content="Ste Brennan - Code Computerlove - http://www.codecomputerlove.com/" />
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<link href="styles.css" type="text/css" rel="stylesheet" />
<link href="css/photoswipe.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" charset="utf-8" src="js/cordova-1.9.0.js"></script>
<script type="text/javascript">
<script type="text/javascript">
(function(window, PhotoSwipe){
document.addEventListener('DOMContentLoaded', function(){
var
options = {},
instance = PhotoSwipe.attach( window.document.querySelectorAll('#Gallery a'), options );
}, false);
}(window, window.Code.PhotoSwipe));
</script>
<script type="text/javascript" src="js/cordova-1.9.0.js"></script>
<script type="text/javascript" src="/libs/klass.min.js"></script>
<script type="text/javascript" src="js/code.photoswipe.jquery-3.0.5.min.js"> </script>
<script type="text/javascript" src="js/code.photoswipe.jquery-3.0.5.js"></script>
<script type="text/javascript" src="js/code.photoswipe-3.0.5.min.js"></script>
<script type="text/javascript" src="js/code.photoswipe-3.0.5.js"></script>
<script type="text/javascript" src="photoswiper.js"></script>
}
Add the js files before the script that you have written, it might be the cause of the problem because the photoswipe features that you are trying to use in the script have not been loaded and hence are not available yet.

Initializing code using pageInit() in jQuery Mobile

I am attempting to initialize javascript for a jQuery mobile page using the pageInit() function. I have outlined some attempts below that I can't seem to get working. Can any one tell me what I'm doing wrong. Also is there a way to do this using $(this) instead of using the pages explicit name?
pageInit.html:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css"
/>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://jqueryui.com/ui/jquery-1.7.1.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.core.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.widget.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.position.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.autocomplete.js"></script>
<script src="http://code.jquery.com/ui/1.8.20/jquery-ui.min.js"
type="text/javascript"></script>
<script src="functions.js"></script>
</head>
<body>
<script>
alert("script");
$(this).live('pageinit', function (event) {
//$('pageInit.html').live( 'pageinit',function(event){
//$('#pageInit').live( 'pageinit',function(event){
//$('#pageInit.html').live( 'pageinit',function(event){
alert("Page Initialized");
});
</script>
</body>
The pageinit event is fired on the document. It doesn't make sense to use live, either. Instead I would use bind. For example:
$(document).bind('pageinit', function (event) {
alert("Page Initialized");
});
Take a look at the jQuery mobile documentation for more information.

Categories