Search code examples
urlencodehtml-encode

HTML encoded links and anchors


I have a use case where I am setting the page focus to a particular element (having an anchor before it). When a user is not signed in, there is a redirect to the login page and after signing in, the user is redirected to the page in question, with the URL encoded.

I see that a URL of the form link#target works as expected (focusing on the element) while the url encoded link link%23target doesn't. Is this expected behavior?

Edit: If this is the expected behavior, is there a work around to focus on the target? As in, a way around url encode?

Edit adding more info:
Assuming that there is a code

page1.html
... html before the anchor ...
<a name="test">Some code</a>
... html after the anchor ...

I am accessing the page as page1.html%23test. This doesn't work the same way as page1.html#test. Is there a jQuery method to implement this? Would location.hash contain test even after it has been url encoded? I have no control on changing the url encoding.

Edit:

As I knew which named anchor I wanted to go to after page is redirected, I did a

window.location.hash = namedAnchor  

to solve the issue. This JS line is output only if a customer is successfully signed in. Solved my issue, though not the generic answer I was looking for. I was looking for a way to avoid escaping of # in url encode.


Solution

  • Yes. Encoding the # as %23 effectively says "I just mean a plain old "#" character, not a URL fragment". The same is true of other reserved characters: escaping them stops them from having special meaning in the URL.

    In your case you do want to encode the URL when passing it to your login page as a parameter, but your login page should decode the URL before performing the redirect.