?
Solved

Help Parsing QueryString Using JavaScript

Posted on 2007-10-15
5
Medium Priority
?
1,994 Views
Last Modified: 2011-09-20
JavaScript Experts,

I need help parsing a querystring that contains an '=' character as apart of the value. For example, mysite.html?key=value=1. I need to break that into:

Key: key
Value: value=1

I normally use split('=') but that wouldn't work in this case. Can anyone help me out? Example, link, or idea would be greatly appreciated.

Best,

~ C
0
Comment
Question by:clickclickbang
  • 3
5 Comments
 
LVL 9

Expert Comment

by:william007
ID: 20079928
Hi, it is better not to using those special keyword like ?,= that used in URL for querystring value.

If you are developing your web application using jsp or asp.net or others, the language has provide you the method for encoding URL or decoding URL.

Basically encoding URL will help you convert all the special character for URL, like = to some other value, decoding URL will then convert it back the querystring that has been encoded.


In javascript there is an similar option, you can call "escape" to encode URL and call "unescape" to decode URL.

alert(escape("java=hello"));
alert(unescape(escape("java=hello")));


Otherwise, it will be very difficult for you to parse the querystring.
0
 
LVL 9

Accepted Solution

by:
william007 earned 2000 total points
ID: 20079995
Correcting typo and add in extra info...

Hi, it is better not to use those special keywords like ?,= that are considered as special character in URL for querystring value.
If you are developing your web application using jsp or asp.net or others, the language has provided you the method for encoding URL or decoding URL.

Basically encoding URL will help you convert all the special character for URL, like =,\,?, etc to some other values(that are not special symbol for URL), decoding URL will then convert it back to the original value.

In javascript there is an similar option, you can call "escape" to encode URL and call "unescape" to decode URL.

alert(escape("java=hello"));
alert(unescape(escape("java=hello")));

Otherwise, it will be very difficult for you to parse the querystring.

But if you really want to insist your method for what ever reason...
There is a way to get around this, you can split by "&". Hence you will get an array of query string.
Then for each item...look for the first "=", before the first "=" is querystring name, after the first "=" is querystring value.
0
 
LVL 1

Author Comment

by:clickclickbang
ID: 20080066
Thanks for the post. I'm a little limited at this point on changing the values but with the information you provided - I now have options and some additional knowledge. Thanks much!
0
 
LVL 9

Expert Comment

by:william007
ID: 20080107
Welcome, glad it help:)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question