So I have a URL that looks like this:
localhost:9031?redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fcallback&scope=test
I want to redirect to the URL localhost:8080/something
which makes use of part of the redirect uri but drops the /callback
from it.
In order to get the redirect uri I have a method that does the following and I pass it the string redirect_uri
:
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
}
This gives me back the string http://localhost:8080/callback
. Then to get the index of callback
I use the following function.
function getPosition(string, subString, index) {
return string.split(subString, index).join(subString).length;
}
Then when the button is clicked I put it all together and do:
$('.button').click(function(e) {
var query = window.location.search.slice(1);
var redirect = getQueryVariable('redirect_uri');
var index = getPosition(redirect, '/', 3);
var baseUrl = redirect.slice(0, index);
window.location.href = baseUrl + '/something';
});
This all works as intended but it doesn't seem particularly foolproof or efficient. Looking for optimizations or suggestions to use functionality of JavaScript or JQuery that I don't know of. Looking to avoid 3rd party libraries but if they are good enough I will definitely still use them.