using fetch on mobile devices - javascript

I am quite new to Javascript and have googled this problem with no joy so was hoping someone could help me with this problem.
I am using fetch to grab a file to update my database.
It all works perfectly on desktops however it does not work at all on mobile.
Using alerts in my function I know it's the fetch not working, as an alert will fire if placed before the fetch but not when placed after.
I have included the polyfill in my code and still have no luck.
Here is my fetch code.
function dbPunch() {
console.log("function fired");
fetch('db-punch.php', {
method: 'GET'
}).then(function(response) {
console.log("fetch worked");
}).catch(function(err) {
console.log("fetch error");
alert("fetch error");
});
}
I have tried it in its simplest form and no luck there.
function dbPunch() {
console.log("function fired");
alert("clicked");
fetch('db-punch.php');
}
Can anyone see what I have done wrong? Thanks

Related

IE is not refresh after call the download function

In the jsp, I use javascript, jquery and json to call a function for download, once the the download is finished, it will return the the current page.
However the problem is although the download is complete, I can able to download and view the file. In the screen, it still show the message indicate it is downloading.
I read this post, the accepted solution mentioned to disable the cache with ajaxSetup. I read my code, I have already include it in the code, however the Internet Explorer still not return to proper page when the download finished. Is there any method I can use to solve the problem. Thank you.
function startDownload() {
$.blockUI({ message: '<h1>Downloading, please wait...</h1>' });
var i = setInterval(function() {
$.ajaxSetup({ cache: false });
$.getJSON("ThePage/downloadProgress?jsoncallback=?",function(download_token) {
if (download_token.fileDownloadToken == "finished" ) {
$.unblockUI();
clearInterval(i);
}
});
}, 1000);
}
Update
I mentioned it occurs on IE because our company is mainly using IE for the web browser. So I intend to make to code works on the IE first. Sorry for the inconvenience that I have made.

Ajax method does not fire on initial page load

Note: Please see edit at the bottom after reading this question.
This issue is only happening in IE 11, and only started occurring after a recent Windows update. There were 5 updates, one of which was specific to IE, so I uninstalled that one. However, the issue still exists. Before I consider rolling back the remaining updates, is there something inherently wrong in my code? The following is inside the document ready function:
$('#leftmenu>li').click(function () {
var clickedId = this.id;
$.ajax({
url: "Session/Index/",
cache: false,
success: function (result) {
if (result.length > 0)
{
performListItemAction(clickedId);
}
else
{
window.location.href = 'Home/Index/'
}
}
});
});
And the following is the performListItemAction method (a separate function not in document.ready):
function performListItemAction(item)
{
alert("clicked");
$(".tabui").each(function ()
{
$(this).hide();
});
$(".listitem").each(function ()
{
$(this).css("background", "transparent");
});
$(document.getElementById(item)).css("background-color", "#C8C8C8");
var targetId = $(document.getElementById(item)).data('target');
var target = $(document.getElementById(targetId));
target.show();
}
The alert clicked never appears when this problem happens, and that is how I concluded the ajax call is not working.
A few other notes:
This issue isn't happening on Firefox.
This only happens if I directly login to the page with a direct URL. If I log in via the application's home screen, and then go to the page that uses the above javascript, the issue doesn't occur.
Thank you.
EDIT: I just now see that the same issue is now occurring in Firefox as well. It's just much less frequent.
After trial and error, I think I fixed the issue by adding a forward slash to the beginning of each of the URLs, and added the type: "POST", to the ajax call. I don't know why it was working fine before, but now this works in all my attempts.

Run process before load page

i am using cordova 6 with onsen-ui,jquery and javascript. So i am trying to make a simple login site, but i need to get if gps is active. I want to know WHERE i must do that validation. Now I do the following on ons.ready() event.
var options = {maximumAge: 0, timeout: 3000, enableHighAccuracy:true};
navigator.geolocation.getCurrentPosition(onGPSCheckSuccess, onGPSCheckError, options);
function onGPSCheckSuccess()
{
console.log("Encontro el GPS Activado");
}
function onGPSCheckError()
{
console.log("Error al chequear el GPS");
gpsDetect.switchToLocationSettings(onSwitchToLocationSettingsSuccess, onSwitchToLocationSettingsError);
}
function onSwitchToLocationSettingsSuccess() {
}
function onSwitchToLocationSettingsError(e) {
console.log("Error al activar el GPS");
alert("Error onSwitchToLocationSettingsError: "+e);
}
So if there a way to do this BEFORE my main page es loaded?
Regards
Actually, you can do a simple page redirect. You can have that code in a .htm file, which you call before your page. When this succeeds, you then store the result using localStorage, then redirect to the page you want, and retrieve the results from localStorage.
I think the best way to do this, it with the following code:
document.addEventListener("init", function(event){
if(event.target.id=='yourHomePageID') {
var options = {maximumAge: 0, timeout: 3000, enableHighAccuracy:true};
navigator.geolocation.getCurrentPosition(onGPSCheckSuccess, onGPSCheckError, options);
}
},false);
By using the init function for the homepage, this actually runs a bit before ons.ready() as discussed here by #fran-dios:
Onsen 2.0 - Adding event listener to Ons-Switch with Javascript
Hope this helps. It is what I am doing based on the aforementioned question and it solved my issue.

How to use JQuery and custom site variables with phantomjs

I'm trying out phantomjs for browser testing and am having a few issues. My current code is below...
var page = require('webpage').create();
page.open('http://address.com', function(){
console.log('page opened');
page.evaluate(function(){
console.log('inside page eval');
varname.varaction(13633);
})
if (jQuery('#bcx_13633_iframe_overlay').is(':visible')){
console.log('it is visible');
} else {
console.log('it is not visible');
}
console.log('made it to exit');
phantom.exit();
})
I've tried using includeJS with a link to jQuery and wrapping that around everything between page.open and phantom.exit but it seems to stall out and skip that portion. I have custom actions located on the site that I can call in the console if i visit it as called in the code listed above, however, it tells me that it can't find the variable name for that either. Anyone with phantomjs experience have any tips on how to fix this?

How can I logout by Facebook JS-SDK?

I need to logout from Facebook from my application.
I tried to watch the other posts, but I didn't find what I need...
I have realised a link likeLOGOUT and I have written a function, but it doesn't work... I tried this 3 methods, but none of them works.
function FB_LOGOUT(){
//FB.logout(function(response) {
// window.location.reload(true);
//});
//FB.Event.subscribe('auth.logout', function(response) {
// window.location.reload();
//});
//FB.logout(function()
//{
// top.location.href = 'http://google.com/';
//});
}
Can you tell me how to logout from Facebook from my application? Please explain everything you think I'd better know, I'm really ignorant in this field... Thank you :)
Sorry I can't write as comment, try this
FB.logout(function(response) {
FB.Auth.setAuthResponse(null, 'unknown');
});
this refers to Facebook JS SDK FB.logout() doesn't terminate user session

Categories

Resources