pzp
pzp

Reputation: 6597

Putting a fragment identifier inside of a query parameter

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?

Upvotes: 0

Views: 877

Answers (1)

robertklep
robertklep

Reputation: 203359

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.

Upvotes: 1

Related Questions