1
\$\begingroup\$

I'm trying to add a link to a page. The link should contain the current URL in a querystring (to be used as a ReturnUrl) and it can contain a hash.

Middle-clicking, right-clicking and ctrl+clicking should be supported. For example, middle-clicking will open the link in a new tab with (or without) the querystring.

Is this completely off-track, or how can I improve it?

<a href="@Url.Action(MVC.Links.ThereAndBackAgain())?ReturnUrl=" onclick="this.href += encodeURIComponent(location.href)">
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 6, 2015 at 18:10
\$\endgroup\$
2

1 Answer 1

1
\$\begingroup\$

I don't think this can possibly be improved. The location hash may be changed by the client after the page is loaded, by clicking on local anchors. This is not visible by the asp.net, and fully visible by the client-side javascript, in the value of location.href. And since you require the complete URL to be included as part of the query string, it must be encoded with encodeURIComponent, which you correctly did.

answered Jul 30, 2017 at 10:18
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.