0

I am trying to redirect to a URL path that contains a query parameter called next, which is itself a URL path that contains a fragment identifier. Here is the JavaScript code I tried:

window.location.href = "/gauth?next=" + window.location.pathname + "#onload=exportToDrive"

Just to make myself clear, the #onload=exportToDrive should belong to the next URL path (not the URL that the browser is redirecting to). How can I specify that information to the browser so that it will handle this situation correctly?

pzp
  • 6,249
  • 1
  • 26
  • 38
  • In general, the entire value of the `next` parameter should be URL-encoded with something like [`encodeURIComponent()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent). – robertklep Jun 22 '16 at 21:16
  • @robertklep Worked like a charm! Thanks so much! If you want to make your comment into an answer, I would gladly give you an upvote and the accepted answer :). – pzp Jun 23 '16 at 02:43

1 Answers1

1

You should always encode URL parameter values properly, using a function like encodeURIComponent:

window.location.href = "/gauth?next=" + encodeURIComponent(window.location.pathname + "#onload=exportToDrive");

This will ensure that any fragment identifiers (but also query string parameters) won't apply to /gauth.

robertklep
  • 198,204
  • 35
  • 394
  • 381