Can anyone explain to me how to get the device's IMEI number from Phonegap?
I am using the IMEI plugin in my app it is not showing anything.
To install, I have tried both:
phonegap plugin add https://github.com/zho/phonegap-imeiplugin.git
-- OR --
cordova plugin add imeiplugin
Example Usage:
window.plugins.imeiplugin.getImei(callback);
function callback(imei) {
console.log("My Android IMEI :" + imei);
I want the output to be displayed when user opens the app, but it is not showing.
here is the solution that how can you use imeiplugin
Index.html
<!DOCTYPE html>
<html>
<body>
<h1 id="demo"></h1>
<script type="text/javascript" src="cordova.js"></script>
<script src="js/index.js"></script>
<script> app.initialize(); </script>
</body>
</html>
Index.js
var app = {
// Application Constructor
initialize: function () {
app.bindEvents();
},
bindEvents: function () {
document.addEventListener('deviceready', app.onDeviceReady, false);
},
onDeviceReady: function () {
window.plugins.imeiplugin.getImei(callback);
}
};
function callback(imei) {
var element=document.getElementById("demo");
element.value=imei;
}
There is something wrong with the plugin you provided it showing an error like imeiplugin is unefined at window.plugins.imeiplugin.getImei(callback);
so i tried with the below and its worked for me
cordova plugin add org.hygieiasoft.cordova.uid
and call the finction on device ready
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
console.log(cordova.plugins.uid.IMEI);
}
check this for more info
Related
I am creating a web app using asp.net mvc. I want to detect a manual refresh by a user,
but I am not able to get the following events
1: beforeunload
2: unload
the following way I Tried
1
$(window).on('beforeunload', function () {
pageCleanup();
});
2
$(window).on("unload", function () {
pageCleanup();
});
function pageCleanup() {
alert(1)
}
3
window.addEventListener("unload", logData);
function logData() {
alert(1)
}
4
$(window).unload(function () {
alert('1');
});
the above is an example with alert, I want to have an ajax call in
unload function
but none of these seems to execute when a user press any kind of refresh(F5, ctrl-shift-R, ctrl-R, from url tab)
what should I try now?
Hi Check Once and Let me know
<html>
<head>
<title>Refresh a page in jQuery</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
</head>
<body>
<h1>Stop a page from exit with jQuery</h1>
<button id="reload">Refresh a Page in jQuery</button>
<script type="text/javascript">
$('#reload').click(function() {
location.reload();
});
$(window).bind('beforeunload', function(){
return '>>>>>Before You Go<<<<<<<< \n bro!!Working';
});
</script>
</body>
</html>
You should use bind for beforeunload:
$(window).bind("beforeunload", () => alert(1));
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
I have been trying for the past few days to get jQuery working in PhoneGap's Hello World template, but no luck. I can't seem to get jQuery to work at all.
In the example below, I added a button to the default Hello World code and I want it to display an alert each time it is clicked. The HTML is:
<html>
<head>
*<!-- different metas and link to css -->*
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript"> app.initialize(); </script>
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>PhoneGap</h1>
<div id = "deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
<br>
<button class = "event clkhere"> Click here </button>
</div>
</div>
</body>
</html>
And the js/index.js file is:
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener("deviceready", this.onDeviceReady,false);
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
var receivedButton = parentElement.querySelector('.clkhere');
receivedButton.setAttribute('style', 'background-color: #008CBA;');
$(".clkhere").click(function() {
alert("button clicked") ;
});
alert("reached this far");
console.log('Received Event: ' + id);
}
};
Above, the difference compared to the default PhoneGap JS is a) My button becomes blue when deviceready, b) The jQuery code, and c) Another alert after the jQuery code to see if it gets triggered. Here, the first alert gets triggered but the button click doesnt work and the second alert doesn't get triggered.
I have tried multiple suggestions about $(document).ready placement, and even tried rearranging the order of the scripts in index.html, but nothing seems to have worked.
Any thoughts would be much appreciated. Thanks
I believe you need to add jquery’s $().ready() around your document.addEventListener(“deviceready”), so that your onDeviceReady() will not trigger until both jQuery and Cordova have completed loading.
The deviceready event is different than other events, in that if the event is already “done”, setting the listener will fire the callback immediately. Setting the deviceready event listener inside jQuery’s $().ready() function will ensure that both Cordova and the DOM/jQuery are ready for your code.
Is it possible to executeScript() on loadstart event in Cordova inAppBrowser? Here is my standalone example I made to try to make it happen:
<!DOCTYPE html>
<html>
<head>
<title>Standalone Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
var iabRef = null;
function testFunction() {
iabRef.executeScript({
code: {'alert("It is alive! ALIVE!")'}
});
}
function onDeviceReady() {
iabRef = window.open('http://telegraph.co.uk', '_self', 'location=no', 'zoom=no', 'hardwareback=yes');
iabRef.addEventListener('loadstart', testFunction);
}
</script>
</head>
<body>
</body>
</html>
Doesn't work for me though. Config.xml allow origin is set to *. Any suggestions?
Thanks!
Yes, it possible to executeScript() on loadstart.Remove {} from alert part as follows:
function testFunction() {
iabRef.executeScript({
code: 'alert("It is alive! ALIVE!");'
});
}
Hope this works
After a little bit of wandering I found that changing "_self" to "_blank" makes it work. So:
iabRef = window.open('http://telegraph.co.uk', '_blank', 'location=no', 'zoom=no', 'hardwareback=yes');
Hello everyone,
I have just recently started learning the require.js , but I don't seem to understand as how does the whole mechanism work...and especialy how do I access the variables/objects created using this javascript library.
Let me explain it further in the example below..
webroot/page.jsp
<html>
<script data-main="js/page" src="js/lib/require.js"></script>
</html>
webroot/js/common.js
requirejs.config({
baseUrl: 'js/lib',
paths: {
app: '../app',
}
});
webroot/js/app/page.js
require(['./common'], function (common) {
require(['./app/page/init'], function(page)
{
page.load(1);
});
});
webroot/js/app/page/init.js
define(['./load'], function (load) {
var LoadPage = new load();
return LoadPage;
});
webroot/js/app/page/load.js
define(function () {
function loadPage() {
}
loadPage.prototype = {
load: function (page_id) {
console.log("Opening page "+page_id+" ");
}
};
return loadPage;
});
now when I run the page everything works perfectly, but now I'd like to execute the load method by clicking on a button.
<div onclick="page.load(2);"> </div>
but this does not work..So I'd like to know if there is any possibility to bind this action to any button or link...whatever..
(apologies for my bad english)
Thanks,
Alex
How about as below assuming you are using JQuery:
<div id="divPageLoad"></div>
<script language="javascript" type="text/javascript">
$("#divPageLoad").on("click", function() {
page.load(2);
});
</script>
I'm starting to use Cordova 3.1.0 now. I use command line to generate a project and then modify the existing code.
I copy and paste the following code from official website to test.
<!DOCTYPE html>
<html>
<head>
<title>InAppBrowser.addEventListener Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
var ref = window.open('http://apache.org', '_blank', 'location=yes');
ref.addEventListener('loadstart', function(event) { alert('start: ' + event.url); });
ref.addEventListener('loadstop', function(event) { alert('stop: ' + event.url); });
ref.addEventListener('loaderror', function(event) { alert('error: ' + event.message); });
ref.addEventListener('exit', function(event) { alert(event.type); });
}
</script>
</head>
<body>
</body>
</html>
This doesn't work. There are no alert. Then I added a button to try to fire the events.
<!DOCTYPE html>
<html>
<head>
<title>InAppBrowser.addEventListener Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
$("#btn").click(function(){
var ref = window.open('http://apache.org', '_blank', 'location=yes');
ref.addEventListener('loadstart', function(event) { alert('start: ' + event.url); });
ref.addEventListener('loadstop', function(event) { alert('stop: ' + event.url); });
ref.addEventListener('loaderror', function(event) { alert('error: ' + event.message); });
ref.addEventListener('exit', function(event) { alert(event.type); });
});
}
</script>
</head>
<body>
<button id="btn">trigger</button>
</body>
</html>
This time, it works with second time. It means, it doesn't work when I click the button first time. But it works at the second time when I click the button. After that, I use console log inside the callback function to debug. The logs were not appeared at the first time (appeared at the second time and third, forth....).
I don't really know why this happened. I followed all steps from official website. Creating project, installing plugin, building and so on.
Could someone give me a hand?
I ran into the same problem. On further investigation, I found that the events weren't firing in the emulator / browser, but fired fine on the Nexus 7 device that I do my testing on. Hope that helps!
It looks like it's a problem with the last phonegap/cordova version 3.1.0.
https://groups.google.com/forum/#!topic/phonegap/e5_5unC2fYs
Try an older version.