anderish
anderish

Reputation: 1759

Javascript Url Replace

So currently, I have my url like this: http://localhost:8000/fund_monitor/fund_details/fundaccountid=4&transmission=3&startDate=2017-08-01&endDate=2017-08-02/

Then when I redirect the url using windows.location.replace(url), the url becomes like this: http://localhost:8000/fund_monitor/fund_details/fundaccountid%3D4&transmission%3D3&startDate%3D2017-08-01&endDate%3D2017-08-02/

So the equal sign gets converted to another format. Is there a way to retain the original format?

Thanks

Upvotes: 0

Views: 61

Answers (2)

Hassan Imam
Hassan Imam

Reputation: 22574

You can use decodeURIComponent().

The decodeURIComponent() function decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.

var url = 'http://localhost:8000/fund_monitor/fund_details/fundaccountid%3D4&transmission%3D3&startDate%3D2017-08-01&endDate%3D2017-08-02/';

console.log(decodeURIComponent(url));

Upvotes: 2

Horia Coman
Horia Coman

Reputation: 8781

It might be because the URL is not in a valid format. It's format is roughly protocol://host:port/path?query_params[1], where query_params looks like a=1&b=2 etc. But you do need the ? to separate the path from your parameters. Whatever you're using seems to treat the part fundaccountid=4&transmission=3&startDate=2017-08-01&endDate=2017-08-02/ as a path, and url encodes it so it can be a proper path. Perhaps try to write the URL as: http://localhost:8000/fund_monitor/fund_details?fundaccountid=4&transmission=3&startDate=2017-08-01&endDate=2017-08-02 and see if that works.

Though it will mean some changes to your backend.


[1] The full format you can see on Wikipedia or RFC 3986

Upvotes: 4

Related Questions