Skip to content

Instantly share code, notes, and snippets.

@thorn
Created May 23, 2012 13:14
Show Gist options
  • Select an option

  • Save thorn/2775179 to your computer and use it in GitHub Desktop.

Select an option

Save thorn/2775179 to your computer and use it in GitHub Desktop.
getting url params with coffeescript and jquery
# 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
@toptierlabs
Copy link
Copy Markdown

Hi, returnVal.lenght and returnVal.lenght should be .length not .lenght

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment