删除url param javascript

时间:2015-12-16 16:51:29

标签: javascript url

我需要制作此网址

http://test.com/deal/134487/some-deal-id?sourceType=by_category&somestring=true&anotherparam=true

看起来像这样

http://test.com/deal/134487/some-deal-id?somestring=true&anotherparam=true

我想只删除sourceType并保留其他所有内容

这就是我到目前为止在网址上看起来效果很好的内容 http://test.com/deal/134487/some-deal-id?sourceType=by_category 但是在sourceType没有任何反应之后添加任何更多的键。此外,我在Google代码管理器中运行此功能,因此无法刷新页面

   (function () {
    var sourceType;
    var query = parseQuery(window.location.search);
    var history = window.history
    delete query.sourceType 
    var search = stringifyQuery(query);
    if (window.location.search) {
      sourceType = window.location.search
          .substr(1)
          .split('&')
          .filter(function (i) {return ~i.indexOf('sourceType=')})
          .map(function (i) {return i.split('=')[1] })[0]
     history && history.replaceState && history.replaceState(history.state, null, window.location.pathname + search)
    }
  })()  

  function parseQuery (locationSearch) {
  if (typeof locationSearch !== 'string' || !locationSearch.length) {
    return {}
  }

  var query = {}
  locationSearch.substr(1).split('&').forEach(function (partStr) {
    var parts = partStr.split('=')
    var key = parts[0]
    var val = decodeURIComponent(parts[1])
    if (query.hasOwnProperty(key)) {
      var existingVal = query[key]
      if (existingVal instanceof Array) {
        existingVal.push(val)
      } else {
        query[key] = [existingVal, val]
      }
    } else {
      query[key] = val
    }
  })
  return query
}

function stringifyQuery (query) {
  if (!query) return ''

  return '?' + Object.keys(query).map(function (key) {
    var val = query[key]
    if (val instanceof Array) {
      return val.map(function (valPart) {
        return [key, encodeURIComponent(valPart)].join('=')
      }).join('&')
    } else {
      return [key, encodeURIComponent(val)].join('=')
    }
  }).join('&')
}

2 个答案:

答案 0 :(得分:0)

无法理解你给出的上述src。如果您想从URL中删除查询字符串,以下内容可以帮助您

window.location.href = window.location.href.split('?')[0];

此行将拆分并为您提供不带查询的URL。

答案 1 :(得分:0)

好的,这似乎有用

所以,如果我的网址是 https://test.com/deal/141788/colored-tempered-glass-iphone-screen-protector?sourceType=by_content&somestring=true&moreparams=true

我希望它看起来像 https://test.com/deal/141788/colored-tempered-glass-iphone-screen-protector?somestring=true&moreparams=true

 function changeUrl()  {
      var sourceType;
      var query = parseQuery(window.location.search);
      var history = window.history
      delete query.sourceType
      var search = stringifyQuery(query);
      history && history.replaceState && history.replaceState(history.state, null, window.location.pathname + search)
      }


  function parseQuery(locationSearch) {
      if (typeof locationSearch !== 'string' || !locationSearch.length) {
          return {}
      }

      var query = {}
      locationSearch.substr(1).split('&').forEach(function(partStr) {
          var parts = partStr.split('=')
          var key = parts[0]
          var val = decodeURIComponent(parts[1])
          if (query.hasOwnProperty(key)) {
              var existingVal = query[key]
              if (existingVal instanceof Array) {
                  existingVal.push(val)
              } else {
                  query[key] = [existingVal, val]
              }
          } else {
              query[key] = val
          }
      })
      return query
  }

  function stringifyQuery(query) {
      if (!query) return ''

      return '?' + Object.keys(query).map(function(key) {
          var val = query[key]
          if (val instanceof Array) {
              return val.map(function(valPart) {
                  return [key, encodeURIComponent(valPart)].join('=')
              }).join('&')
          } else {
              return [key, encodeURIComponent(val)].join('=')
          }
      }).join('&')
  }