Kolichikov
Kolichikov

Reputation: 3030

stop window.location.replace from decoding url string

I have a url that needs to be mapped to a specific format, where some slashes should remain as %20f. So for instance:

http://myUrl.com/section1/section2/section%2fnamewithslash/?somequery=1

Such a url should keep the %2f in the section name. Unfortunately, I can't change this to remove the slash from the section name.

The issue is that when I call window.location.replace, it changes that %20f to a slash, which throws off the link.

You can see a fiddle example here https://jsfiddle.net/Kolichikov/k480rnff/ To see what I mean, follow the link, and right click on the results frame and click view frame source. You'll the see the url in the browser.

So my question is how do I replace the current resource with the one from the url without decoding the link?

Thank you for any help.

Upvotes: 1

Views: 1711

Answers (2)

lapint
lapint

Reputation: 112

Perhaps this will do what you want it to?

https://jsfiddle.net/ewzvnbre/3/

<a href="javascript:redirect()"> Go to Link </a>

<script> function redirect(){ window.location.replace('http://somehost.xyzas/section1/thing%2fwith%20slash/?qsp=1') } </script>

Upvotes: 1

user4513271
user4513271

Reputation:

Do an onclick event instead of processing the link in href.

<script>
  <a href="#" onclick="window.location.replace('http://myUrl.com/section1/section2/section%2fnamewithslash/?somequery=1');">
</script>

One advantage of this over using javascript in the href attribute is that it will display the link in the status bar, rather than a run script.

Upvotes: 1

Related Questions