JQuery - Using navigator.notification.alert - jquery-mobile

I have a requirement for a popup to appear with a custom heading (having it appear from index.html on an app just looks tacky).
I tried the suggestion at the end of the following link:
Custom JavaScript alerts in iOS using PhoneGap HTML
So I added the code below to my index.html in the script section:
function showMessage(message, callback, title, buttonName){
title = title || "default title";
buttonName = buttonName || 'OK';
if(navigator.notification && navigator.notification.alert){
navigator.notification.alert(
message, // message
callback, // callback
title, // title
buttonName // buttonName
);
}else{
alert(message);
callback();
}
}
UPDATE
I have the following code for the alert;
if ((inputNumber>maxAllowed))
{
showMessage("The input is too high.",null,"Warning","Warning");
}
After compiling the app, this is not working.
The following is in index.html:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
// Now safe to use the PhoneGap API
}
<function shown above is here>
Any idea why this is still not working? Showing from index.html
Thank you.
Kind Regards,
Gary Shergill

This error tells you that function navigator.notification don't exist.
Usually this is because:
Phonegap/Cordova is not initialized inside a HEAD
Function is not initialized inside a deviceready event. Basically function can't be called before cordova.js is fully initialized.
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
// Now safe to use the PhoneGap API
}

Here is a function I use while testing Phonegap applications on my PC. I remove it when deploying app on mobile device. It's for confirm function, but you can adjust it for alerting and so on.
// TODO: remove on deploy
navigator.notification = {
confirm: function (message, successCallback) {
successCallback(1);
}
};

You are testing in a browser so navigator.notification is undefined. Also It seems that you added the function showMessage but you are not using it. Try with:
showMessage("The value is too high!", null,"Warning", "Warning");

From phone, notice that the callback is not a string. So in your function, you pass it a string and that is causing it a problem.
http://docs.phonegap.com/en/1.0.0/phonegap_notification_notification.md.html
navigator.notification.alert(
'You are the winner!', // message
alertDismissed, // callback
'Game Over', // title
'Done' // buttonName
);
I see this is the case because I am trying to do this also. So unfortunately you cannot override the callback, and you need to "hard Code" it.

I have add the plugin using CLI like :
$ cordova plugin add cordova-plugin-dialogs
and its working fine for me.

Related

UI not updating on 'pageinit', probably timing issue

I have the following .js file for my project. This is running in a regular browser, using jquery 1.9.x and jquerymobile 1.3.1. The init function below appears to be running when the page loads and the UI is not updated. Though... I can copy the function into the console and run it, and the UI updates as it is supposed to, so this in not a case of incorrect file paths, or incorrect ids for the UI elements, but I suspect timing. I am also NOT using cordova or phone gap in this instance.
So, my question is, why is the UI not updating when the $(document).bind('pageinit', ...) function is called? If I put a breakpoint in the init method, it is getting called when the page loads. Any suggestions on using a different event or approach?
var simulator = simulator || {};
(function (feedback, $, undefined) { 'use-strict';
feedback.init = function () {
$.get('feedback-config.xml', function (data) {
$('#feedback-to').val($(data).find('email').text());
$('#feedback-subject').val($(data).find('emailSubject').text());
$('#feedback-display').html($(data).find('message').text());
$('#feedback-form').attr('action', $(data).find('serverurl').text()).ajaxForm({success: function () {
alert("Thank you for your feedback!");
}, error: function () {
alert("We're having difficulties sending your feedback, sorry for the inconvenience.");
}});
});
};
}(simulator.feedback = simulator.feedback || {}, jQuery));
$(document).bind('pageinit', function () { 'use strict';
simulator.feedback.init;
});
Thanks in advance.
Found it, simple mistake... In the 'pageinit' function I am calling simulator.feedback.init; instead of simulator.feedback.init(); Not sure why JSLint didn't pick that up initially, but it pointed it out when I tried again later. Thanks.

Open local html with InAppbrowser

I've been looking for ways to open a html local levels, Local I mean that the html is inside the www folder of the application of phonegap, with inappbrowser.
var about = window.open("About.html", "_blank", "location=yes");
This is the line of code with which I intend to do this, but apparently does not work, if someone could help me I would be very grateful.
Are you using Phonegap 3.0.0? I think this is a bug with this Phonegap version.
I have been using this as a workaround (opening an inappbrowser instance):
// Image zoom
$('#container').on('tap', '#content.cmscontent img', function() {
var browser = window.open((navigator.userAgent.indexOf('Android') != -1 ? 'file:///android_asset/www/' : '') + encodeURI($(this).attr('src')), '_blank', 'location=no,toolbar=yes,enableViewportScale=yes,transitionstyle=crossdissolve');
});
See I added (navigator.userAgent.indexOf('Android') != -1 ? 'file:///android_asset/www/' : '') as a prefix before the url. Now it detects when you're app is on Android and adds the local URL in front of it.
Thanks for your answers, and achieves it work apparently the problem is that there was cleaning the project and for this reason not added the www about.html
But now I have another problem, as I will have already seen pretending to show a about of the application, for which show the ios version and device name. for this I am using the properties of the object name and version device.That for some reason unknown to me but that are not available in the index file, that I decided to pass these attributes to html url about, for example:
var about = window.open("About.html?"+device.name+"&"+device.version, "_blank", "location=yes");
and about.html handles these variables with:
var cadGET = location.search.substr(1,location.search.length);
but does not show the html inappbrowser and location bar only shows loading ...
know if the inappbrowser supports pass url parameters?
The code that you have written is correct. it is opening page in inappbrowser.
Now the thing is you have to make sure that you call it after device is ready.
Also check you are using 2.x version of cordova framework. and still if you getting issue with the inappbrowser please provide some more information.
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var about = window.open("about.html", "_blank", "location=yes");
console.log('Received Event: ' + id);
}
};

Worklight app crashes on Windows 8

I'm developing an app using Worklight 5.0.6. The app is targeted at tablets (iOS, Android and Windows). The app works fine on iOS and Android, but I'm having trouble getting it to run properly on Windows 8. The app crashes when I click on a link. Here's part of the error message:
"HTML1701: Unable to add dynamic content ' <link/><table></table><a href='/a'>a</a><input type='checkbox'/>'. A script attempted to inject dynamic content or elements previously modified dynamically that might be unsafe. For example, using the innerHTML property to add script or malformed HTML will generate this exception. Use the toStaticHTML method to filter dynamic content or explicitly create elements and attributes with a method such as createElement. For more information, see http://go.microsoft.com/fwlink/?LinkID=247104."
The app is supposed to inject a html fragment when a link is clicked. I'm using the following the following functions to inject html into an element:
function loadPartial(path, elementId)
{
$.get(path, function (data) {
$(elementId).html(data).trigger("create");
$("#my-navbar > ul").removeClass("ui-grid-a");
if (!hideDuringFocus)
{
$("[data-role=header]").fixedtoolbar({ hideDuringFocus: "" });
$("[data-role=footer]").fixedtoolbar({ hideDuringFocus: "" });
}
});
}
function loadPartialWithFunction(path, elementId, aFunction)
{
$.get(path, function (data) {
$(elementId).html(data).trigger("create");
$("#my-navbar > ul").removeClass("ui-grid-a");
if (!hideDuringFocus)
{
$("[data-role=header]").fixedtoolbar({ hideDuringFocus: "" });
$("[data-role=footer]").fixedtoolbar({ hideDuringFocus: "" });
}
aFunction();
});
}
Is there a mistake I made in the code? Any help would be appreciated. Please let me know if more information or source code is needed.
The issue has been resolved, thanks. I have to wrap the code with MSApp.execUnsafeLocalFunction so it'll look this:
function loadPartialWithFunction(path, elementId, aFunction)
{
$.get(path, function (data) {
MSApp.execUnsafeLocalFunction(function(){
$(elementId).html(data).trigger("create");
$("#my-navbar > ul").removeClass("ui-grid-a");
if (!hideDuringFocus)
{
$("[data-role=header]").fixedtoolbar({ hideDuringFocus: "" });
$("[data-role=footer]").fixedtoolbar({ hideDuringFocus: "" });
}
aFunction();
});
});
}
This is an issue with jQuery on Win8 Metro. Metro apps have dynamic content restrictions, that need to be bypassed first. Here is a stack overflow question with lots of answers for this issue:
Using jQuery with Windows 8 Metro JavaScript App causes security error

Cordova 2.3.0 Open links in Safari on iOS

I know this has been asked a lot, but i tried almost anything I could find here and nothing helps.
I want to open links with class "external" in native iOS browser, but i'm not able to manage it.
Here is what I tried:
$('.external').live('click', function(event) {
event.preventDefault();
var url = $(this).attr('href');
loadURL(url);
})
function loadURL(url){
var ref = window.open(url, '_system');
}
As of phonegap documentation, it should now open the link in system webbrowser, but it always uses the InAppBrowser, no matter of the link-target.
I also set "OpenAllWhitelistURLsInWebView" to false in config.xml. No change.
Can you double check that your click handler is actually being invoked? If you put the code that attaches the event handler in the wrong location(i.e. in onDeviceReady(), then the click handler will not be invoked) -> the links will always be opened inside of the app. Also I am using jquery ".on" since ".live" has been removed in jquery 1.9; which I was using for my test.
I just tested the following code on Cordova 2.4.0, and it works. Here is some important code excerpts:
HTML portion:
<a class=".external" href="http://www.google.com">Go to external page</a>
JAVASCRIPT portion:
function loadURL(url){
var ref = window.open(url, '_system');
}
$(function(){
$('.external').on('click', function(event) {
event.preventDefault();
var url = $(this).attr('href');
loadURL(url);
});
});

JqueryMobile Loading spinner for a function call

I would like to show the loading spinner while a function is running in jquerymobile.
The function is non ajax its just going to calculate some values and save to a sqlite db.
I have searched and i keep finding the same examples saying use $.mobile.showPageLoadingMsg(); etc.
example
function loading()
{
$.mobile.showPageLoadingMsg();
//Do Some Stuff in here
$.mobile.hidePageLoadingMsg();
}
Thanks In advance.
Lmac
You can simply call:
$.mobile.showPageLoadingMsg();
at the beginning of your function as you mentioned but on a callback of your function
$.mobile.hidePageLoadingMsg();
If you wanted the loader to appear for a certain period of time you could use:
$.mobile.showPageLoadingMsg();
var wait = setTimeout(function(){
$.mobile.hidePageLoadingMsg();
},400);
Use $.mobile.loading( 'show' ); and $.mobile.loading( 'hide' ); instead. Functions above are deprecated from jquery mobile documentation.

Resources