A JavaScript function parses URL parameters into JSON objects _javascript tips

Source: Internet
Author: User

Question: Write a JavaScript function parsequerystring, which is used to parse the URL parameter into an object.
Eg:var obj=parsequerystring (URL);

Create three types of objects:
One:

var person=new Object ();
Person.name= "Sun";
person.age=24;

Two:

var person=new Object ();
person["name"]= "Sun";
person["Age"]=24;

Three:
An object literal expression

var person={
Name: "Sun",
age:24
}

Ps:
1. In this example, it is more appropriate to use the second form to add elements to obj
2, Split ("&"), if the URL has only one parameter, there is no "&" time, also will not error, will return only array[0]

function parsequerystring (URL)
{
var obj={};
var keyvalue=[];
var key= "", value= ""; 
var parastring=url.substring (Url.indexof ("?") +1,url.length). Split ("&");
for (var i in parastring)
{
keyvalue=parastring[i].split ("=");
KEY=KEYVALUE[0];
VALUE=KEYVALUE[1];
Obj[key]=value; 
} 
return obj;
}

Related Article

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.