window.location + manipulating string url from window.location.href - javascript

I'm trying to get my facebook graph oauth setup using javascript's window location methods.
Here is my code so far:
function fbLog() {
var clientID = '11502353444318540727';
var redirecturi = 'http://google.com';
var clientSecret = '6987d02323442423231f8b9da767b060e9';
var codeURI;
$('#fbLogin').click(function() {
window.location = 'https://graph.facebook.com/oauth/authorize?client_id=' + clientID + '&display=touch&redirect_uri='+ redirecturi + '&type=user_agent';
codeURI = window.location.href;
codeURI.split('=');
console.log(codeURI);
codeURI = codeURI.split('=');
codeURI = codeURI[1];
console.log(codeURI);
codeURI = codeURI.split('#');
codeURI = codeURI[0];
console.log('mega fun');
window.open = 'https://graph.facebook.com/oauth/access_token?client_id=' + clientID + '&redirect_uri=' + redirecturi + '&client_secret=' + clientSecret + '&code=' + codeURI
});
}
when I set codeURI to window.location.href, is that original call back page dead?
I'm super confused. Have I switched to a different window when I try to get the codeURI?

You have to assume that as soon as you set window.location, the browser will load a new page and all code on the current page will stop running. Don't try to do anything afterwards.
As far as I can tell, all you need here is to do things in a different order.

Related

Add params to URL if page gets reloaded

Heyho, I want to add GET params like?USER_NAME=Max&USER_ID=01 to the URL if the page gets reloaded. I already got a few snippets for changing the URL / adding params. But I need something to detect the page reload and execute a function. Or a way to change the path of the Url directly if the page gets reloaded.
I tried several things like beforeunload and navigation types.
$(window).bind('beforeunload', function() {
// EDIT: vars like uname etc are defined
var newurlgetstring = "?USER_NAME=" + uname + "&USER_EMAIL=" + uemail + "&LAST_NAME=" + lname + "&PRE_NAME=" + fname + "&UTITEL=" + utitel + "&U_ID_T=" + uidt + "&MV=" + mv + "&V=" + uv ;
var newurl = window.location.protocol + "//" + window.location.host +window.location.pathname + newurlgetstring;
window.history.pushState({path:newurl},'',newurl);
});
But I want able to execute a function with beforeunload. I was just able to display a return question.

window.location.replace() not working [duplicate]

I want to navigate to a URL on my site. This is what I have:
var TheDemoURL = window.location.host;
if (SomeCondition1) { TheDemoURL = TheDemoURL + '/fr/demo'; }
if (SomeCondition2) { TheDemoURL = TheDemoURL + '/de/demo'; }
...
window.location.replace(TheDemoURL);
Initially, in the variable watch, I have TheDemoURL: "localhost:49173" and when I alert the final TheDemoURL is looks a good URL but in reality nothing happens.
Why is this not working?
Ok, for those who come here, the solution was to add this:
var TheDemoURL = window.location.protocol + '//' + window.location.host;
Not sure if this is specific to asp.net but it made it work.
Try using
self.location = TheDemoURL;
This will take into account iframes and other weirdness.

authentication issues from getting a json result

i got a problem with my login script in a platform of creating native mobile app
the server side work perfectly
here is my test with postman
and this is the function that should redirect me to another page bit it don't
function auth() {
var login = js.getProperty('username', 'value');
var password = js.getProperty('password', 'value');
var url = 'http://meanlast.gear.host/user/login';
url = url + '&login=' + login + '&password=' + password;
var response = js.getDataFromUrl(url);
var jsonRep = JSON.parse(response);
if (jsonRep == "Login successful!") {
js.alert("ok");
js.navigateToPage('menu');
} else {
js.alert("error");
}
}
Your code
var url = 'http://meanlast.gear.host/user/login';
url = url + '&login=' + login + '&password=' + password;
produces
"http://meanlast.gear.host/user/login&login=login&password=password"
Do you see the problem with your querystring? You do not have a ?

Open an URL followed by a hash sign using location.href or similar

There is a page with a "share at whatsapp" button. This creates and executes an URL like:
whatsapp://send?text=Some text followed by a link - http://link_to_this_page#something
The problem is that the browser (I've tested only with Chrome for now) automatically deletes from the hash sign to onwards.
I have tried the basic:
var href = 'whatsapp://send?text=Example text - ';
var uri = location.protocol + '//' + location.host + location.pathname + '#gm.';
location.href = href + uri;
I tried too with location.replace(), location.assign() and window.open() with no luck.
So the question is, how can I do? It's imperative to use the hash because it tells to the target page that it has to do some things in javascript (which could take more time to change).
You should be encoding anything that is in the query string.
location.href = href + encodeURIComponent(uri);
You probably should be doing:
var href = 'whatsapp://send?text=';
var text = 'Example text - ';
var uri = location.protocol + '//' + location.host + location.pathname + '#gm.';
location.href = href + encodeURIComponent(text + uri);

What is wrong with my foursquare api call?

The live example is here
http://kenziejoy.github.io/frontend-nanodegree-map/
I'm trying to pull data about locations that I have hard coded in an array - either by their foursquare id (didn't seem to be working) or their lat and lng. (client ID and secret are variables I just haven't shown them here)
I don't need any other functionality than just pulling data from their database to display on a map so I thought it would fall under the userless access but it is giving me an error that the request are bad because I don't have the proper authentication.
Thanks in advance
From the foursquare site
"Userless access
Some of our endpoints that don’t pertain to specific user information, such as venues search are enabled for userless access (meaning you don’t need to have a user auth your app for access). To make a userless request, specify your consumer key's Client ID and Secret instead of an auth token in the request URL.
https://api.foursquare.com/v2/venues/search?ll=40.7,-74&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&v=YYYYMMDD
To see what level of permissions each endpoint needs, check out the filters at the top of our endpoints page."
/**********FourSquare***************/
$.ajax({
url:'https://api.foursquare.com/v2/venues/search',
dataType: 'json',
data: 'limit=1' +
'&ll='+ placeItem.lat() +','+ placeItem.lng() +
'&?client_id='+ CLIENT_ID +
'&client_secret='+ CLIENT_SECRET +
'&v=20140806' +
'&m=foursquare',
async: true,
success: function (data) {
var result = data.response.venue;
var contact = result.hasOwnProperty('contact') ? result.contact : '';
if (contact.hasOwnProperty('formattedPhone')) {
placeItem.phone(contact.formattedPhone || '');
}
var location = result.hasOwnProperty('location') ? result.location : '';
if (location.hasOwnProperty('address')) {
placeItem.address(location.address || '');
}
var bestPhoto = result.hasOwnProperty('bestPhoto') ? result.bestPhoto : '';
if (bestPhoto.hasOwnProperty('prefix')) {
placeItem.photoPrefix(bestPhoto.prefix || '');
}
if (bestPhoto.hasOwnProperty('suffix')) {
placeItem.photoSuffix(bestPhoto.suffix || '');
}
var description = result.hasOwnProperty('description') ? result.description : '';
placeItem.description(description || '');
var rating = result.hasOwnProperty('rating') ? result.rating : '';
placeItem.rating(rating || 'none');
var url = result.hasOwnProperty('url') ? result.url : '';
placeItem.url(url || '');
placeItem.canonicalUrl(result.canonicalUrl);
// Infowindow code is in the success function so that the error message
// Content of the infowindow
var contentString = '<div id="iWindow"><h4>' + placeItem.name() + '</h4><div id="pic"><img src="' +
placeItem.photoPrefix() + '110x110' + placeItem.photoSuffix() +
'" alt="Image Location"></div><p>Information from Foursquare:</p><p>' +
placeItem.phone() + '</p><p>' + placeItem.address() + '</p><p>' +
placeItem.description() + '</p><p>Rating: ' + placeItem.rating() +
'</p><p><a href=' + placeItem.url() + '>' + placeItem.url() +
'</a></p><p><a target="_blank" href=' + placeItem.canonicalUrl() +
'>Foursquare Page</a></p><p><a target="_blank" href=https://www.google.com/maps/dir/Current+Location/' +
placeItem.lat() + ',' + placeItem.lng() + '>Directions</a></p></div>';
// Add infowindows
google.maps.event.addListener(placeItem.marker, 'click', function () {
infowindow.open(map, this);
// Bounce animation
placeItem.marker.setAnimation(google.maps.Animation.BOUNCE);
setTimeout(function () {
placeItem.marker.setAnimation(null);
}, 800);
infowindow.setContent(contentString);
});
},
// Alert the user on error.
error: function (e) {
infowindow.setContent('<h5>Foursquare data is unavailable.</h5>');
document.getElementById("error").innerHTML = "<h4>Foursquare data is unavailable. Please try refreshing.</h4>";
}
});
I took a look at the live example URL and you were getting a lot of bad request errors in the JavaScript console in Chrome.
Looking at these, you had a bad URL, you were using:
https://api.foursquare.com/v2/venues/search?limit=1&ll=45.5589522,-122.6517163&?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&v=20140806&m=foursquare
The problem seems to be that you have:
&?client_id
which makes the URL invalid.
Changing this to
&client_id
fixes this and I then see data coming back from Foursquare.

Categories

Resources