Last active
August 29, 2015 14:06
-
-
Save paraggupta1993/13cda29a92a0421eaa32 to your computer and use it in GitHub Desktop.
Adding a parameter to URL string using Coffeescript.
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
@addAttributeToUrl: (url, key, val) -> | |
return url if _.some arguments, (arg) -> | |
arg == null or arg == '' | |
#regex = (all non-# or non-?)(0-1: # or ?)(all non-blanks) | |
regex = /([^\?#]*)([?#]?)([^\s]*)/ | |
match = regex.exec(url.trim()) | |
baseUrl = match[1] | |
separator = match[2] | |
queryString = match[3] | |
url = "#{baseUrl}?#{key}=#{val}" | |
if queryString | |
separator = "&" if separator is "?" | |
url += separator + queryString | |
url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment