how to fix navigator.permissions.query - javascript

I have a navigator.permissions.query that is not working
function handlePermission() {
navigator.permissions.query({name:'geolocation'}).then(function(result) {
if (result.state == 'granted') {
report(result.state);
geoBtn.style.display = 'none';
} else if (result.state == 'prompt') {
report(result.state);
geoBtn.style.display = 'none';
navigator.geolocation.getCurrentPosition(revealPosition,positionDenied,geoSettings);
} else if (result.state == 'denied') {
report(result.state);
geoBtn.style.display = 'inline';
}
result.onchange = function() {
report(result.state);
}
});
}
function report(state) {
document.getElementById("t").innerHTML = ('Permission ' + state);
}
handlePermission();
I have a live example here please check out my code and tell me where I am going wrong

You should read the example again and the docs for the functions you're using. The error messages in you live example are pretty clear.
navigator.geolocation.getCurrentPosition(revealPosition,positionDenied,geoSettings);
You have to hand the correct/valid parameters to this function. revealPosition is a function which is run, when the user granted the geolocation, positionDenied is a function which is run, when the user denies the access to the geolocation and geoSettings is an object which includes the setting for the call to .getCurrentPosition.
So once you create the missing parameters instead of handing undefined references to the call the function will run just fine.

Related

How to get user location on browser using PHP [duplicate]

My application is powered by jQuery mobile and uses geolocation.
After my application attempts to get the user's location, the (Chrome) browser prompts the user:
Example.com wants to track your physical location [allow] [deny]
My goal is:
If the user clicks "Allow", function 1 is called (location is used
by app).
If the user clicks "Deny", function 2 is called (address form
appears).
How can I bind a function to the event that occurs (if any) when the user clicks the "Allow" or "Deny" button?
The getCurrentPosition function accepts two function arguments. Heck, the first is executed when you allow and the other when you deny!
Documentation
http://jsfiddle.net/pimvdb/QbRHg/
navigator.geolocation.getCurrentPosition(function(position) {
alert('allow');
}, function() {
alert('deny');
});
Link to Docs
function handlePermission() { navigator.permissions.query({name:'geolocation'}).then(function(result) {
if (result.state == 'granted') {
report(result.state);
geoBtn.style.display = 'none';
} else if (result.state == 'prompt') {
report(result.state);
geoBtn.style.display = 'none';
navigator.geolocation.getCurrentPosition(revealPosition,positionDenied,geoSettings);
} else if (result.state == 'denied') {
report(result.state);
geoBtn.style.display = 'inline';
}
result.onchange = function() {
report(result.state);}});}function report(state) {
console.log('Permission ' + state);}
I hope this works.

Different redirects in function of multiple referer

Now I have this code of redirect in function of the referer:
document.addEventListener('DOMContentLoaded', function() {
console.log(isMobile);
var referrer = document.referrer;
if(referrer.indexOf('site1.com') !== -1 || referrer.indexOf('site2.com') !== -1) {
if(isMobile.phone) {
window.location = "http://www.landingphone.com";
console.log('Is phone');
} else if(isMobile.tablet) {
window.location = "http://www.landingtablet.com";
console.log('Is tablet');
} else {
window.location = "http://www.landingdesktop.com";
console.log('Is desktop');
}
} else {
window.location = "http://www.anotherlanding.com";
}
});
Its ok this code for redirect in function of the referer site1 and site2.com, but if I need redirect also another referer (for example site3.com) to another landing (for example www.landingphone2.com, landingtablet2.com and landingdesktop2.com). What I need add in the code? what i need modify?
Thank you very much.
You can try something like this, adding another else if statement as referred to in the comment to your answer.
document.addEventListener('DOMContentLoaded', function() {
console.log(isMobile);
var referrer = document.referrer;
if (referrer.indexOf('site1.com') !== -1 || referrer.indexOf('site2.com') !== -1) {
if (isMobile.phone) {
window.location = "http://www.landingphone.com";
console.log('Is phone');
} else if (isMobile.tablet) {
window.location = "http://www.landingtablet.com";
console.log('Is tablet');
} else {
window.location = "http://www.landingdesktop.com";
console.log('Is desktop');
}
} else if (referrer.indexOf('site3.com') !== -1) {
// Do your other redirects here
} else {
window.location = "http://www.anotherlanding.com";
}
});

Uncaught TypeError: Cannot read property 'toLowerCase' of null

This prompt was working perfectly well up until I updated some other javascript. I don't know how I messed it up. This function is declared in the body tag to run 'onload'.
function funcPrompt() {
var answer = prompt("Are you a photographer?", "Yes/No");
answer = answer.toLowerCase();
if (answer == "yes") {
alert('Excellent! See our links above and below to see more work and find contact info!');
}
else if(answer == "no") {
alert('That is okay! See our links above and below to learn more!');
}
else if(answer == null || answer == "") {
alert('Please enter an answer.');
funcPrompt();
}
else {
alert('Sorry, that answer is not an option');
funcPrompt();
}
}
Now suddenly I'm getting this error and the prompt won't appear.
Not certain why you are getting a null but if you are trying to avoid the null, use this:
answer = (answer?answer:'').toLowerCase();
If we click on Cancel, prompt will return null and one can not apply toLowerCase on null(Will cause an exception!)
Add a condition answer===null before all other conditions and return to stops the execution of a function
function funcPrompt() {
var answer = prompt("Are you a photographer?", "Yes/No");
if (answer === null || answer === "") {
alert('Please enter an answer.');
funcPrompt();
return;
}
answer = answer.toLowerCase();
if (answer == "yes") {
alert('Excellent! See our links above and below to see more work and find contact info!');
} else if (answer == "no") {
alert('That is okay! See our links above and below to learn more!');
} else {
alert('Sorry, that answer is not an option');
funcPrompt();
}
}
funcPrompt();
In your case, better use a confirm instead of a prompt
function funcConfirm() {
var answer = confirm("Are you a photographer?");
if (answer === true) {
alert('Excellent! See our links above and below to see more work and find contact info!');
} else {
alert('That is okay! See our links above and below to learn more!');
}
}
funcConfirm();

Communicate from Serial port -> Chrome app -> webpage. Webpage initiates.

like the question asks, I'm trying to communicate with a serial device through a chrome app, via a webpage. The objective is to turn on a switch with a button on a webpage, and make sure the switch is in fact on (serial response).
So far I have been able to turn on the switch fine, however I need to validate that it is in fact enabled.
My chrome app code:
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
if (request.request == 'info') {
sendResponse(DEVICE_INFO);
} else if (request.request == 'turn_off') {
device_array.forEach(function(device){
if (device.id == request.device_id){
device.send('f');
}
});
//INSTEAD OF "OK" I NEED IT TO ASK THE DEVICE TO CONFIRM OFF/ON STATUS
sendResponse('OK');
} else if (request.request == 'turn_on') {
device_array.forEach(function(device){
if (device.id == request.device_id){
device.send('n');
}
});
sendResponse('OK');
}
return true;
});
If I send a "status" query to the device, it's going to take a few milliseconds for it to respond "off" or "on" to the serial buffer. Any ideas on how to go about this? Thanks in advance.
Ultimately I got it to work by requesting the device status within the listener block of code. Below are the modifications, crude but it gets the job done. DEVICE_STATUS is being updated by an onReceive listener on the serial connection.
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
if (request.request == 'info') {
sendResponse(DEVICE_INFO);
} else if (request.request == 'turn_off') {
device_array.forEach(function(device){
if (device.id == request.device_id){
device.send('f');
}
});
var time_loop = 0;
connection.send('s'); // s is for STATUS
var timer = setInterval(device_is_off, 200);
function device_is_off(){
if (time_loop > 5){ //Serial Communication Timeout at 1sec
sendResponse('ERROR ' + DEVICE_STATUS);
clearInterval(timer);
return;
}
if (DEVICE_STATUS == 0){
sendResponse('OK');
clearInterval(timer);
return
}
else time_loop++;
}
} else if (request.request == 'turn_on') {
device_array.forEach(function(device){
if (device.id == request.device_id){
device.send('n');
}
});
var time_loop = 0;
connection.send('s'); // s is for STATUS
var timer = setInterval(device_is_on, 200);
function device_is_on(){
if (time_loop > 5){
sendResponse('ERROR ' + DEVICE_STATUS);
clearInterval(timer);
return;
}
if (DEVICE_STATUS == 1){
sendResponse('OK');
clearInterval(timer);
return
}
else time_loop++;
}
}
return true;
});

facebook login validation jQuery return true / false

I have a simple facebook validation i would like to implement to my jQuery button.
When the user clicks the button it should check if logged in, if TRUE then change the text.
I found this article which talks about returning a true/false state but when i tried to implement it in my code it didnt work.
Any suggestion where im going wrong please, thank you.
function fb_loginCheck(){
FB.getLoginStatus(function(response, e) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
e.returnValue = true;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook, but has not authenticated your app
fb_oAuth();
e.returnValue = false;
} else {
// the user isn't logged in to Facebook.
fb_oAuth();
e.returnValue = false;
}
}, true);
}
$('.myBttn').click(function(){
var io = return fb_loginCheck();
if (io){
$this = $(this).text();
if($this == 'yes')
$(this).text('no');
else
$(this).text('yes');
}
return false;
});
got it to work:
similar to potench answer but removing the e.returnValue did it
function fb_loginCheck(callBack){
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
callBack(true);
} else if (response.status === 'not_authorized') {
fb_oAuth();
callBack(false);
} else {
fb_oAuth();
callBack(false);
}
}, true);
}
Something like this might work. I've moved the methods around so they get fired when a response is returned from the FB.getLoginStatus method.
I'm passing in a callBack method which fires when the response from FB.getLoginStatus returns a result. Also note that I've had to re-scope the $(this) variable.
function fb_loginCheck(callBack){
FB.getLoginStatus(function(response, e) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
e.returnValue = true;
callBack(true);
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook, but has not authenticated your app
fb_oAuth();
e.returnValue = false;
callBack(false);
} else {
// the user isn't logged in to Facebook.
fb_oAuth();
e.returnValue = false;
callBack(false);
}
}, true);
}
$('.myBttn').click(function(){
var targ = $(this);
fb_loginCheck(function (io) {
targ.text( (io) ? "yes" : "no" );
});
return false;
});

Categories

Resources