Skip to content

Instantly share code, notes, and snippets.

@Nully
Last active February 20, 2018 05:58
Show Gist options
  • Save Nully/1375966 to your computer and use it in GitHub Desktop.
Save Nully/1375966 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