https://github.com/kylefox/jquery-querystring
A jQuery plugin to make working with querystrings a breeze
https://github.com/kylefox/jquery-querystring
Last synced: about 1 year ago
JSON representation
A jQuery plugin to make working with querystrings a breeze
- Host: GitHub
- URL: https://github.com/kylefox/jquery-querystring
- Owner: kylefox
- Created: 2011-04-13T16:01:24.000Z (about 15 years ago)
- Default Branch: master
- Last Pushed: 2014-11-02T19:24:05.000Z (over 11 years ago)
- Last Synced: 2025-04-18T18:33:10.727Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 187 KB
- Stars: 41
- Watchers: 3
- Forks: 12
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
This plugin attempts to make working with querystrings easier. The primary function exposed is `$.querystring`, which handles both parsing & serialization (depending on the type of argument passed in). A `$.fn.querystring` method is also provided, which makes getting & setting the querystring of `` elements more convenient.
_Currently only the simplest cases work, but the goal is to support nested objects and arrays. Please refer to the test suite for more info._
Basic usage
-----------
Objects can be serialized to a querystring fragment:
$.querystring({name: "John", age:42}); // "name=John&age=42"
Conversely, querystring fragments can be parsed back into objects:
$.querystring("name=John&age=42"); // {name: "John", age:42}
Element methods
---------------
You can access an element's querystring directly:
// Markup: Search for bikes!
$('a').querystring(); // {terms: 'bicycles'}
and modify it, too:
$('a').querystring({perPage: 10});
// Search for bikes!
You'll notice the new parameter was merged into the existing querystring. You can optionally pass a second argument (boolean) indicating if the existing querystring should be cleared:
$('a').querystring({foo: 'bar'}, true);
// Search for bikes!
You can also use this to remove the querystring:
$('a').querystring({}, true);
_Right now only `` tags are supported, but support for `` tags is planned._
Syntax
------
The syntax for objects and arrays is similar to that of [Rack](http://rack.rubyforge.org/). For example, an object like this:
{person: {name: "Johnny"}}
equates to a string like this:
"person[name]=Johnny"
Arrays are similar:
{drinks: ["Whisky", "Beer", "Wine"]}
equates to:
"drinks[]=Whisky&drinks[]=Beer&drinks[]=Wine"
Tests & Contributing
--------------------
Testing is done with [QUnit](http://docs.jquery.com/Qunit). Have a look at [tests/tests.js](https://github.com/kylefox/jquery-querystring/blob/master/tests/tests.js) to learn more about the intended API. Running the tests (ie, opening `tests/index.html` in your browser) will give you an idea of which features remain to be implemented & clarified.
I would love help with this plugin, so please fork & submit pull requests. Here's an immediate hit-list of things to implement:
* Parsing & serialization of nested objects & arrays
* Coercion to native types (ex: Number, Boolean)
* Handling of blank/empty values