url - How can I get query string values in JavaScript? -


is there plugin-less way of retrieving query string values via jquery (or without)?

if so, how? if not, there plugin can so?

you don't need jquery purpose. can use pure javascript:

function getparameterbyname(name, url) {     if (!url) url = window.location.href;     name = name.replace(/[\[\]]/g, "\\$&");     var regex = new regexp("[?&]" + name + "(=([^&#]*)|&|#|$)"),         results = regex.exec(url);     if (!results) return null;     if (!results[2]) return '';     return decodeuricomponent(results[2].replace(/\+/g, " ")); } 

usage:

// query string: ?foo=lorem&bar=&baz var foo = getparameterbyname('foo'); // "lorem" var bar = getparameterbyname('bar'); // "" (present empty value) var baz = getparameterbyname('baz'); // "" (present no value) var qux = getparameterbyname('qux'); // null (absent) 


note: if parameter present several times (?foo=lorem&foo=ipsum), first value (lorem). there no standard , usages vary, see example question: authoritative position of duplicate http query keys.


this update based on new urlsearchparams specs achieve same 2 lines of code:

#browser url: http://www.example.com/?anything=123 var searchparams = new urlsearchparams(window.location.search); //?anything=123 console.log(searchparams.get("anything")) //123 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -