How to pass a query parameter along with a redirect in JavaScript -


right redirection made saying

window.location = "/relative_path/"; 

what want add query parameter along redirection, destination page can @ that, , after action taken redirect further page.

example use login system. when user's token expires, redirected login page. @ time of redirection want pass along exact url (including query parameters in url) login page, after successful login user redirected point he/she was.

i tried pass path url parameter, doesn't work because of escaping issues: redirect url uses same characters (?, =, ...), confuses system, , parameters truncated.

so that, doesn't work:

window.location = "/login?redirect=/original_location?p1=vl1&p2=v2 

any suggestion appreciated.

you can use encodeuricomponent()

the encodeuricomponent() method encodes uniform resource identifier (uri) component replacing each instance of characters one, two, three, or 4 escape sequences representing utf-8 encoding of character (will 4 escape sequences characters composed of 2 "surrogate" characters).

example

window.location = "/login?redirect=" + encodeuricomponent("/original_location?p1=vl1&p2=v2") 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -