Javascript: how to programmatically kill an alert after X seconds - javascript

When asking for permission for geolocation the user gets a popup window asking for geolocation permission.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
}
Lets say the user doesn't respond after 5 seconds, I would like to "force the errorCallback".
How would I do so?
Thanks.

According the MDN, the third argument to getCurrentPosition() is an options object that allows you to specify a timeout value.
The documentation specifies that the timeout value is the max time allowed to wait for the GPS position, so you'd have to test it to see if it also includes the time waiting for the user to respond to a permission prompt.

I guess, it's more or less up to the browser how to handle the fact that the user is not replying on the question about permission.
Anyhow, a workaround could be a redirect after a given time:
<html>
<body>
<h1>My page</h1>
<script>
var userdidnothing=false;
function TimeoutHandler() {
if (!userdidnothing) {
window.location.href="http://www.yoursecondpage.com";
}
}
setTimeout("TimeoutHandler();",5000);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function() {
userdidnothing=true;
alert("SUCCESS");
},
function() {
alert("ERROR");
});
}
</script>
Geolocation feature is helpful, if you need to know where you are...
</body>
</html>

Related

How to always prompt for geolocation in browser once blocked? [duplicate]

im building an app through phonegap, with a geolocation button.
if a user denies permission for geolocation the first time, how can i ask for permission again when they click the geolocation button again?
my code structure at the moment is:
function getLocation() {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, positionError);
} else {
hideLoadingDiv()
showError('Geolocation is not supported by this device')
}
}
function positionError() {
hideLoadingDiv()
showError('Geolocation is not enabled. Please enable to use this feature')
}
You can't.
The only thing you can do is to display the instructions to reactivate the location sharing in his browser's settings (https://support.google.com/chrome/answer/142065?hl=en).
Two ways of doing this:
If you have a version of Chrome bigger than 83.0.4103.97 then use the lock icon in the URL
For older versions of Chrome the bellow code will work fine:
The bellow code only works on Chrome.
Steps:
Open Chrome
Open the console
Copy in the console
var allowGeoRecall = true;
var countLocationAttempts = 0;
Copy in the console the functions
function getLocation() {
console.log('getLocation was called')
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,
positionError);
} else {
hideLoadingDiv()
console.log('Geolocation is not supported by this device')
}
}
function positionError() {
console.log('Geolocation is not enabled. Please enable to use this feature')
if(allowGeoRecall && countLocationAttempts < 5) {
countLocationAttempts += 1;
getLocation();
}
}
function showPosition(){
console.log('posititon accepted')
allowGeoRecall = false;
}
Run the function in the console
getLocation();
After running this you will be asked to allow to share your position. If your response is negative you will be asked again until you agree.
HINT: If your user has a negative response, let him know why you need the coordinates. Is vital for him to understand that this step is vital for the good run of the web app.
This can be reset in Page Info which can be accessed by clicking the lock icon next to the URL and allowing Location

Chrome API's onBeforeRedirect not working properly?

I am developing a Google Chrome extension, and I need to detect redirects so I can perform a certain action (the action is irrelevant, this question simply pertains to the redirect). Thus far I have this code in my background.js:
chrome.webRequest.onBeforeRedirect.addListener(function (url, tabId) {
console.log("This is a redirect");
chrome.tabs.sendMessage(tabId, {"message": url}, function(response){});
}, {urls: ["<all_urls>"]});
However, neither the console.log or sendMessage method is getting called. I tried going to wikipedia.com, google.net, and several other sites that I know redirect the user. Why isn't the extension picking this up?
(And yes, I have put "webRequest" in my permissions under my manifest.json file.)
Thanks in advance, please let me know if you need any other code.
EDIT: Thanks to #ze it's working now, but now it's working a little too well. In other words, it's starting to fire now multiple times per page, and when I begin to type the url of a redirect site into the chrome search bar, it also gives me the redirect message. How do I only get it to fire once, when I actually push enter to navigate to the redirect site (and not while i'm still typing)? Here's the new code:
chrome.webRequest.onBeforeRedirect.addListener(function (details) {
if(details.frameId === 0){
alert("This is a redirect.");
// chrome.tabs.sendMessage(details.tabId, {"message": details.url}, function(response){});
}
}, {urls: ["<all_urls>"]});
From what I can see your callback for onBeforeRedirect has two arguments while here says that the callback should have one argument: details. Then you access url and tabId using details.url (or details.redirectUrl if you need the new url) and details.tabId.
Also, frames inside a tab may redirect as well so the event might fire more than once. If the code has to run only when the main frame redirects you should add a check like:
if (details.frameId == 0){
//Your code here
}
Also, I am not sure why you have {urls: ["<all_urls>"]} in the arguments of the addListener function. The only argument is the callback.
So, I would write something like:
chrome.webRequest.onBeforeRedirect.addListener(function (details) {
if(details.frameId == 0){
console.log("This is a redirect");
chrome.tabs.sendMessage(details.tabId, {"message": details.url}, function(response){});
}
});
Let me know if this worked out.

ask for geolocation permission again if it was denied

im building an app through phonegap, with a geolocation button.
if a user denies permission for geolocation the first time, how can i ask for permission again when they click the geolocation button again?
my code structure at the moment is:
function getLocation() {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, positionError);
} else {
hideLoadingDiv()
showError('Geolocation is not supported by this device')
}
}
function positionError() {
hideLoadingDiv()
showError('Geolocation is not enabled. Please enable to use this feature')
}
You can't.
The only thing you can do is to display the instructions to reactivate the location sharing in his browser's settings (https://support.google.com/chrome/answer/142065?hl=en).
Two ways of doing this:
If you have a version of Chrome bigger than 83.0.4103.97 then use the lock icon in the URL
For older versions of Chrome the bellow code will work fine:
The bellow code only works on Chrome.
Steps:
Open Chrome
Open the console
Copy in the console
var allowGeoRecall = true;
var countLocationAttempts = 0;
Copy in the console the functions
function getLocation() {
console.log('getLocation was called')
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,
positionError);
} else {
hideLoadingDiv()
console.log('Geolocation is not supported by this device')
}
}
function positionError() {
console.log('Geolocation is not enabled. Please enable to use this feature')
if(allowGeoRecall && countLocationAttempts < 5) {
countLocationAttempts += 1;
getLocation();
}
}
function showPosition(){
console.log('posititon accepted')
allowGeoRecall = false;
}
Run the function in the console
getLocation();
After running this you will be asked to allow to share your position. If your response is negative you will be asked again until you agree.
HINT: If your user has a negative response, let him know why you need the coordinates. Is vital for him to understand that this step is vital for the good run of the web app.
This can be reset in Page Info which can be accessed by clicking the lock icon next to the URL and allowing Location

HTML5 geolocation - error callback doesn't always fire

I'm using the Geolocation API in this fairly simple way:
if (navigator && navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
console.log("gettingPosition: " + position);
myFunction(position(;
}, function(error){
console.log('did not get position');
console.log(error);
});
}
In some cases (Firefox 12.0 is the browser I have in front of me) no callback fires if the user clicks "Not Now", not even the error. Is this expected? If so, is my only option to follow the technique in Geolocation feedback while accepting the request and set my own timeout?
This appears to be by design (for Firefox). See Josh's comments in https://bugzilla.mozilla.org/show_bug.cgi?id=635175
The logic is that clicking "not now" is a way for the user to dismiss the permission dialog without committing to a decision about whether or not to allow geolocation permissions.
It looks like FF16 and Chrome-Latest both operate this way.
(P.S. Don't shoot the messenger. :p)

Get GeoLocation In The Browser

I have the following JavaScript in my page to get the location of the user:
state.currentTimer = navigator.geolocation.watchPosition(success, error, { enableHighAccuracy: true, maximumAge: 5000 });
It is intended that the page is called from the browser on a mobile phone. However the watchPosition method seems incredibly unreliable. Sometimes it won't ever get a location, other times the location is massively off, sometimes it will work fine and then just stop.
I have tried to eliminate the problem of the phones signal by testing it in a city with a good signal.
Is there a better way to get the location from a mobile while in the browser?
I use :
navigator.geolocation.getCurrentPosition(
function(pos) {
pos.coords.latitude;
pos.coords.longitude;
pos.coords.accuracy;
},
function(error) {
error.code;
}
);
All infos are here http://dev.w3.org/geo/api/spec-source.html
Really, that's the only way right now. To ensure that you get more accurate results, you can check the accuracy property of the object passed to your success handler.

Categories