query string formatting in Http-url
A GET request is the most common type of request, and the common language queries the server for some information. If necessary, you need to append the query string to the end of the URL. Sometimes it is necessary to read the query string in the URL in order to render different pages depending on the query string in the URL. How to avoid formatting errors in the URL writing query string times and how to read the query string in the URL more quickly is the problem that this blog will solve.
HTTPS://WWW.BAIDU.COM/BAIDU?WD=%C6%E2%C7%E0%F1%C6+CSDN&TN=MONLINE_DG in this URL? The string WD=%C6%E2%C7%E0%F1%C6+CSDN&TN=MONLINE_DG is the query string.
Please go to GitHub to view the relevant code.
( function(window, document) { varUrlparas = function(URL) { returnUrlParas.fn.init (URL); }; Urlparas.version =' 1.0.0 '; Urlparas.fn = Urlparas.prototype = {URL:"", Pathname:"", paras:""Init: function(URL) { This. url = URL; This. Pathname = Url.split ("?")[0]; This. paras = This. get ();return This; },//Returns the URL parameter and its value as Object typeGet function(option) { varPARASTR, paras, url = This. URL;if(URL) {parastr = Url.split ("?")[1];if(PARASTR) {paras = {}; Parastr = Parastr.split ("&"); for(varNinchPARASTR) {varName = Parastr[n].split ("=")[0];varValue = Parastr[n].split ("=")[1]; Paras[name] = value; } }Else{return{}; }if(!option) {returnparas; }Else{returnParas[option]? Paras[option]:""; } } },//Reset URL parameter value, if no parameter is created, delete if parameter assignment is nullSet function(option) { varI, name, Val;if(arguments. length = =2) {name =arguments[0]; val =arguments[1]; option = {Name:val}; }if("string"===typeofOption) { This. paras[option] =""; }Else if("Object"===typeofOption) { for(IinchOption) {if(Option[i] = = =NULL) {Delete This. Paras[i]; }Else{ This. paras[i] = Option[i]; } } }Else{ }return This. build (); },//delete URL in the specified parameter to return the new URLRemove function(option) { varIif("string"===typeofoption) {option = Option.split (","); for(IinchOption) {Delete This. Paras[option[i]]; } }return This. build (); },//URL and paras re-artifact URLsBuild function() { varI, Newurl = This. Pathname +"?"; for(Iinch This. paras) {Newurl + = (i +"="+ This. paras[i] +"&"); }returnNEWURL.SUBSTR (0, Newurl.length-1); } }; UrlParas.fn.init.prototype = Urlparas.fn; Window.urlparas = Urlparas; }) (window, document);//UseTesturl ="HTTPS://WWW.BAIDU.COM/BAIDU?WD=%C6%E2%C7%E0%F1%C6+CSDN&TN=MONLINE_DG"; Console.log (Urlparas (Testurl). get ()); Console.log (Urlparas (Testurl). Get ("Lang")); Console.log (Urlparas (Testurl). Set ("Test2","22222")); Console.log (Urlparas (Testurl). Set ("111","Bean")); Console.log (Urlparas (Testurl). Set ({"Ajax":"OK","Lang":NULL,"Trswq":NULL})); Console.log (Urlparas (Testurl). Set ({zcsdf:"ZCSDF", Cesahi:"Ceadasdads"Lang:"ZH-CN"})); Console.log (Urlparas (Testurl). Remove ("Lang,tt")); Console.log (Urlparas (testurl). Pathname);
Execution results
As can be seen, urlparas the test URL to intercept the query characters, increase the query string, delete the query string and other operations.
Note: Here is a query parameter if there is a Chinese, then must encode the problem, workaround please check my blog
query string formatting in Http-url