-
-
Save thbar/3357799 to your computer and use it in GitHub Desktop.
getting url params with coffeescript and jquery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Returns get parameters. | |
# | |
# If the desired param does not exist, null will be returned | |
# | |
# To get the document params: | |
# @example value = $(document).getUrlParam("paramName"); | |
# | |
# To get the params of a html-attribut (uses src attribute) | |
# @example value = $('#imgLink').getUrlParam("paramName"); | |
jQuery.fn.extend | |
getUrlParam: (strParamName) -> | |
strParamName = escape(unescape(strParamName)) | |
if $(this).attr("nodeName") is "#document" | |
if window.location.search.search(strParamName) > -1 | |
qString = window.location.search.substr(1,window.location.search.length).split("&") | |
else if $(this).attr("src") isnt "undefined" | |
strHref = $(this).attr("src") | |
if strHref.indexOf("?") > -1 | |
strQueryString = strHref.substr(strHref.indexOf("?") + 1) | |
qString = strQueryString.split("&") | |
else if $(this).attr("href") isnt "undefined" | |
strHref = $(this).attr("href") | |
if strHref.indexOf("?") > -1 | |
strQueryString = strHref.substr(strHref.indexOf("?") + 1) | |
qString = strQueryString.split("&") | |
else | |
return null | |
return null unless qString | |
returnVal = (query.split("=")[1] for query in qString when escape(unescape(query.split("=")[0])) is strParamName) | |
if returnVal.lenght is 0 | |
null | |
else if returnVal.lenght is 1 | |
returnVal[0] | |
else | |
returnVal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment