URL Address bar Parameters <==> objects (converting objects to address bar parameters and converting address bar parameters to objects) use functions

Source: Internet
Author: User

/** * @author Web wins * @param {object} obj need to stitch Parameter object * @return {String} **/functionObj2qs (obj) {if(!obj &&!)Object.keys (obj). length) {        return""; } Else {        vararr = [];  for(varKeyinchobj) {Arr.push (Key+ "=" +Obj[key]); }        returnArr.join ("&"); }}/** * @author Web Victory * @param {String} URL URL Address bar * @return {Object}*/functionqs2obj (URL) {varQS = Url.split ("?") [1]; vararr = []; varres = {}; if(!Qs) {        //return res;}Else{arr= Qs.split ("&");  for(vari = 0, len = arr.length; i < Len; i++) {            varKey = Arr[i].split ("=") [0]; varval = arr[i].split ("=") [1]; Res[key]=decodeURIComponent (Val); }    }    returnRes;}

Obj2qs is a parameter that converts parameters (object type parameters) to the Address bar, which can be used on Ajax and jump pages.

Qs2obj is the address bar parameter, you can call this function when you need to use the address bar parameter. The return value is an object.

/** * @author Web wins * @param {object} obj needs to stitch the parameter object * @return {String} * */function Obj2qs (obj) {if (!obj &&! Object.keys (obj). length) {return "";} else {var arr = [];for (var key in obj) {Arr.push (key + "=" + Obj[key]);} Return Arr.join ("&");}}
/** * @author Web Victory * @param {String} URL URL Address bar * @return {Object} */function qs2obj (URL) {var qs = url.split ("?") [1];var arr = [];var res = {};if (!QS) {//return res;} else {arr = Qs.split ("&"); for (var i = 0, len = arr.length; I &l T Len i++) {var key = Arr[i].split ("=") [0];var val = arr[i].split ("=") [1];res[key] = decodeURIComponent (val);}} return res;}

URL Address bar Parameters <==> objects (converting objects to address bar parameters and converting address bar parameters to objects) use functions

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.