queryString

2019-12-27  本文已影响0人  泠泉
      function getQueryString (name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
        if (window.location.search.indexOf('=') > -1) {
          var r = window.location.search.substr(1).match(reg)
          if (r) {
            return unescape(r[2])
          }
        }
        return ''
      }

      function addQueryString (url, name, value) {
        var currentUrl = url.split('#')[0]
        if (/\?/g.test(currentUrl)) {
          if (/name=[-\w]{4,25}/g.test(currentUrl)) {
            currentUrl = currentUrl.replace(/name=[-\w]{4,25}/g, name + '=' + value)
          } else {
            currentUrl += (currentUrl.endsWith('?') ? '' : '&') + name + '=' + value
          }
        } else {
          currentUrl += '?' + name + '=' + value
        }
        if (url.split('#')[1]) {
          return currentUrl + '#' + url.split('#')[1]
        } else {
          return currentUrl
        }
      }

      function deleteQueryString (url, name) {
        var reg = new RegExp('(|&)' + name + '=([^&/#]*)')
        var str = url.match(reg)
        if (str && str[0]) {
          var newurl = url.replace(str[0], '')
          return newurl.replace('?&', '?').replace('?#', '#')
        }
        return url
      }
上一篇 下一篇

猜你喜欢

热点阅读