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)">
-
\$\begingroup\$ I believe you can skip the JS, and just append the URL in asp.net using the request object stackoverflow.com/q/5304782/575527 \$\endgroup\$Joseph– Joseph2015年10月07日 00:51:17 +00:00Commented Oct 7, 2015 at 0:51
-
\$\begingroup\$ @JosephtheDreamer Hash should be preserved. Updated the answer (see this stackoverflow.com/questions/3664257/… or this stackoverflow.com/questions/317760/…) \$\endgroup\$thomius– thomius2015年10月07日 04:48:50 +00:00Commented Oct 7, 2015 at 4:48
1 Answer 1
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.