Javascript 获取查询字符串中重定向uri的子部分

Javascript 获取查询字符串中重定向uri的子部分,javascript,jquery,redirect,Javascript,Jquery,Redirect,我有一个URL,看起来像这样: localhost:9031?重定向_uri=http%3A%2F%2localhost%3A8080%2Fcallback&scope=test 我想重定向到URLlocalhost:8080/something,它使用了部分重定向uri,但从中删除了/callback 为了获得重定向uri,我有一个执行以下操作的方法,并向其传递字符串redirect\u uri: function getQueryVariable(variable) { var quer

我有一个URL,看起来像这样:

localhost:9031?重定向_uri=http%3A%2F%2localhost%3A8080%2Fcallback&scope=test

我想重定向到URL
localhost:8080/something
,它使用了部分重定向uri,但从中删除了
/callback

为了获得重定向uri,我有一个执行以下操作的方法,并向其传递字符串
redirect\u 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]);
    }
  }
}
然后单击按钮时,我将其全部放在一起并执行以下操作:

$('.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';
});

这一切都按预期进行,但似乎不是特别简单或有效。寻找使用JavaScript或JQuery功能的优化或建议,我不知道。希望避免使用第三方库,但如果它们足够好,我肯定会继续使用它们。

我建议让浏览器来解析URL。假设您拥有查询字符串中的解码重定向URI,则可以执行以下操作:

var myUrl='1〕http://localhost:8080/callback&scope=test';
函数解析url(url){
var a=document.createElement(“a”);
a、 href=url;
返回{
协议:a.协议,
主机名:a.hostname,
港口:a.port,
路径名:a.pathname
};
}
var parsedURL=parseURL(myUrl);

log(parsedURL.protocol+'/'+parsedURL.hostname+':'+parsedURL.port+'/something')这是基于众所周知的用JavaScript解析URL的方法,由John Long介绍

function parseURL(url) {
  var parser = document.createElement('a'),
      searchObject = {},
      queries, split, i;
  // Let the browser do the work
  parser.href = url;
  // Convert query string to object
  queries = parser.search.replace(/^\?/, '').split('&');
  for( i = 0; i < queries.length; i++ ) {
    split = queries[i].split('=');
    searchObject[split[0]] = decodeURIComponent(split[1]);
  }
  // return all fragments
  return {
    protocol: parser.protocol,
    host: parser.host,
    hostname: parser.hostname,
    port: parser.port,
    pathname: parser.pathname,
    search: parser.search,
    params: searchObject,
    hash: parser.hash
  };
}

// using the method
(function() {
  // your first url
  var temp = parseURL('localhost:9031?redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fcallback&scope=test');
  // second (the redirect_uri param);
  var url = parseURL(temp.params.redirect_uri);

  // now you could do:
  window.location.href = url.protocol + '://' + url.host + '/whateverYoWant';
})();
函数解析url(url){
var parser=document.createElement('a'),
searchObject={},
查询、拆分、i;
//让浏览器来完成这项工作
parser.href=url;
//将查询字符串转换为对象
querys=parser.search.replace(/^\?/,'').split('&');
对于(i=0;i
function parseURL(url) {
  var parser = document.createElement('a'),
      searchObject = {},
      queries, split, i;
  // Let the browser do the work
  parser.href = url;
  // Convert query string to object
  queries = parser.search.replace(/^\?/, '').split('&');
  for( i = 0; i < queries.length; i++ ) {
    split = queries[i].split('=');
    searchObject[split[0]] = decodeURIComponent(split[1]);
  }
  // return all fragments
  return {
    protocol: parser.protocol,
    host: parser.host,
    hostname: parser.hostname,
    port: parser.port,
    pathname: parser.pathname,
    search: parser.search,
    params: searchObject,
    hash: parser.hash
  };
}

// using the method
(function() {
  // your first url
  var temp = parseURL('localhost:9031?redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fcallback&scope=test');
  // second (the redirect_uri param);
  var url = parseURL(temp.params.redirect_uri);

  // now you could do:
  window.location.href = url.protocol + '://' + url.host + '/whateverYoWant';
})();