[removed].search query as JSON

后端 未结 10 946
北海茫月
北海茫月 2020-12-24 14:51

Is there a better way to convert a URL\'s location.search as an object? Maybe just more efficient or trimmed down? I\'m using jQuery, but pure JS can work too.



        
10条回答
  •  悲&欢浪女
    2020-12-24 15:52

    In case someone is looking just to access the search query parameters, use this function:

    function getQueryStringValue (key)
    {
        return decodeURIComponent(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURIComponent(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"))
    }
    

    Easy to use just call getQueryStringValue(term)

提交回复
热议问题