parsing - How to read GET data from a URL using JavaScript? -


I am trying to send data from one page to another.

www.mints.com Name = something

How can read names using javascript?

  function parseURLParams (url) {var queryStart = url.indexOf ("?") + 1, queryEnd = url.indexOf ("#") + 1 || Url.length + 1, query = url.slice (queryStart, queryEnd - 1), pairs = query.replace (/ \ + / g, "") .split ("&"), parameters = {}, i , N, v, nv; If (query === url || query === "") returns; For (i = 0; i and lt; pairs langti; i ++) {nv = pairs [i] .split ("=", 2); N = decoder component (NV [0]); V = decoderioconpent (NV [1]); If (Parms.hasOwnProperty (n)) Parameters [n] = []; Params [N]. Push (NV length === 2? V: faucet); } Return Params; }  

Use this as:

  var urlString = "http://www.foo.com/bar?a=a+a& B% 20b = b & c = 1 & c = 2 & d # hash "; UrlParams = parseURLParams (urlString);  

which gives an object like this:

  {"a": ["aa"], / * always return the ultimate value as an array * / "Bb": ["b"], / * can be special characters with the ultimate names * / "c": ["1", "2"] / * A URL can be the ultimate multiple times ! * / "D": [null] / * is set to clear the parameter without values ​​* /}  

then

  ParseURLParams ("www.mints.  

returns

  {name: [" something "]}  

Edit P> Edit : Used a regex-based approach to url-parsing. It used a small function, but the view was wrong and I found it to be a proper parser Changed with.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -