美文网首页
queryString

queryString

作者: 泠泉 | 来源:发表于2019-12-27 09:40 被阅读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
      }

相关文章

网友评论

      本文标题:queryString

      本文链接:https://www.haomeiwen.com/subject/guvroctx.html