Git Product home page Git Product logo

jquery-plugin-query-parser's Introduction

====================================== jQuery Plugin - Query Parameter Parser

This is a plugin that adds two functions to the jQuery global object for parsing query parameters. It will automatically decode URI encoded values and restore any spaces that may have been replaced with the plus sign.

You can see the plugin in action at:

http://jsfiddle.net/mattsnider/spHAD/

$.parseQuery(s)

This function accepts a query string and returns a JavaScript object on which the query string key/value pairs are applied. So passing the following query string:

$.parseQuery('?&key1=value1&key2=value2');

Will return the object:

{
	key1: 'value1',
	key2: 'value2'
}

$.getQuery()

This is a shortcut method that passing window.location.search to $.parseQuery and cached the result. So calling $.getQuery on the url http://www.myserver.com/?&foo=bar&foofoo=baz will return:

Will return the object:

{
	foo: 'bar',
	foofoo: 'baz'
}

Improperly Formatted Queries

The regex is robust enough handle lots of improperly formatted strings.

Good values, wrapped with crap

The regex can find properly formatted query parameters anyplace in a string, even if proceeded and succeed with crap:

$.parseQuery('?jjsdlgalgdja&foo=bar&adfasdfasdfasdfa');

Will return the object:

{
	foo: 'bar'
}

Doesn't start with ? or &

The function checks if the first letter in the query string is ?, removing it only as necessary, so the following three strings behave identically:

$.parseQuery('?&foo=bar');
$.parseQuery('&foo=bar');
$.parseQuery('foo=bar');

Returning the object:

{
	foo: 'bar'
}

Keys without values are dropped

If you have a key without a value, it is not written to the object:

$.parseQuery('?&foo=bar&baz');

Will return the object:

{
	foo: 'bar'
}

You can change this behavior by providing the tolerant option, so empty keys will be set to empty string ('':

$.parseQuery('?&foo=bar&baz', {tolerant: 1});

Will return the object:

{
	baz: '',
	foo: 'bar'
}

License

http://opensource.org/licenses/MIT

jquery-plugin-query-parser's People

Contributors

mattsnider avatar

Watchers

Nirav Faraswami avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.