There is a link eg. http://localhost:4200/test/test%20link, where %20 is encoded space. When I copy this link and try to open in new tab, the %20 will switch to %2025. How to avoid this issue?
openWindow() {
const link = 'test link';
window.open(
`https://window-open-with-angular.stackblitz.io/${link}`,
'Independent Window',
this.windowFeatures.join()
);
Case scenario: Somebody sents a link, in which is encoded space. When receiver tries to open the link, it crashes due to unwanted encoding.
Here is an example
The space character is encoded as %20 and the % character is encoded as %25.
The way you get %2520 is when the url already has a %20 in it get encoded again to produce %2520
If you copy-paste http://localhost:4200/test/test link you would only get %20, as wanted.
If you copy-paste http://localhost:4200/test/test%20link you would get %2520.
Do not encode your link twice when it is already encoded. Send your link with the raw spaces.
Answer above does not answer the issue.
What was the issue, which caused double encoding? At the top of the appplication was made routing with Angular Router, which was encoding url while logging user.
How the problem was solved? A method called decodeURIComponent() was used to solve the problem.
router.navigate(urlPath) -> router.navigate(decodeURIComponent(urlPath)