Skip to content

Instantly share code, notes, and snippets.

@pgmot
Forked from Nully/jquery.query-parameter.js
Created December 21, 2015 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgmot/fbe92776fbb828822ca8 to your computer and use it in GitHub Desktop.
Save pgmot/fbe92776fbb828822ca8 to your computer and use it in GitHub Desktop.
URLの?以降(GET値)を取得する
// jQuery版に書き換えた
(function($){
var queries = (function(){
var s = location.search.replace("?", ""),
query = {},
queries = search.split("&"),
i = 0;
if(!s) return null;
for(i; i < queries.length; i ++) {
var t = queries.split("=");
query[t[0]] = t[1];
}
return query;
})();
$.queryParameter = function(key) {
return (queries == null ? null : queries[key] ? queries[key] : null);
};
})(jQuery);
// 使い方
// URLが次のような場合
// http://hogehoge.com/sample.js?theme=blue
// $.queryParameter("theme"); // blueという文字列が取得できる
// 指定したキーがなければ...
// $.queryParameter("hage"); // nullを返す
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment