query string formatting in Http-url

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.