In a web application that makes use of AJAX calls, I need to submit a request but add a parameter to the end of the URL, for example:
Original URL:
If you have a string with url that you want to decorate with a param, you could try this:
urlstring += ( urlstring.match( /[\?]/g ) ? '&' : '?' ) + 'param=value';
This means that ? will be the prefix of the parameter, but if you already have ? in urlstring
, than & will be the prefix.
I would also recommend to do encodeURI( paramvariable )
if you didn't hardcoded parameter, but it is inside a paramvariable
; or if you have funny characters in it.
See javascript URL Encoding for usage of the encodeURI
function.