0

I have an url:

var url = /company/overview

I have query params

var params = {
    campaign = "back-to-work"
    user = "1"
}

How can I convert these two values (originally extracted from $location.path() and $location.search()) into one query parameter called return_to. I will later attach this return_to query parameter to a login url.

Github do something similar when trying to access content that requires login:

https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Forgs%2FCompany%2Fdashboard%3Fcampaign%3Dback-to-work
user1283776
  • 19,640
  • 49
  • 136
  • 276

1 Answers1

0

You can use

encodeURIComponent($location.path())

which returns the following for this page

"%2Fquestions%2F39034879%2Fconvert-url-and-query-parameters-into-one-query-parameter"

You just add this as a param to you other url and you're good to go :)

Additional Info:
There are two encoding options in JavaScript:

  • encodeURI() will not encode: ~!@#$&*()=:/,;?+'

  • encodeURIComponent() will not encode: ~!*()'

See Best practice: escape, or encodeURI / encodeURIComponent for more details.

Community
  • 1
  • 1
Resley Rodrigues
  • 2,218
  • 17
  • 17