Instantly share code, notes, and snippets.

Embed
What would you like to do?
クエリーストリングをハッシュに変換 jQuery Plugin
////////////////////////////////////////////////////////////////////////////////
// Query string convert to hash - jQuery Plugin
// クエリーストリングをハッシュに変換
//
// Copyright 2010, Minoru Nakanow
// Licensed under the MIT licenses.
// http://www.opensource.org/licenses/mit-license.html
//
// Usage:
// $.toObject("index.html?width=500&height=auto");
//
////////////////////////////////////////////////////////////////////////////////
(function($) {
//--------------------------------------------------------------------------
// jQuery extend
//--------------------------------------------------------------------------
jQuery.extend({
/**
* Query string convert to hash
*
* @param {String} string "?"を含んだURLなどの文字列
* @return {Object}
*/
toObject: function(string) {
var result = {};
var query = string.substring(string.indexOf("?")+1);
var param = query.split("&");
for (var i=0; i<param.length; i++) {
var hash = param[i].split("=");
result[hash[0]] = hash[1];
}
return result;
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment