How to get Url Hash (#) from server side
Asked Answered
C

6

146

I know on client side (javascript) you can use windows.location.hash but could not find anyway to access from the server side. I'm using asp.net.

Cassel answered 25/11, 2008 at 15:43 Comment(3)
did you get a way aruond this problem , I have bookmarks with has in the url and want to access the text after hash on server side ?Chink
The answers explain that this is not avail on server, because is only interpret by user agent. I was trying to change the active tab, which I was trying to do that on server side. I ended up doing it on client side instead.Cassel
Possible duplicate of Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?Stalactite
D
143

We had a situation where we needed to persist the URL hash across ASP.Net post backs. As the browser does not send the hash to the server by default, the only way to do it is to use some Javascript:

  1. When the form submits, grab the hash (window.location.hash) and store it in a server-side hidden input field Put this in a DIV with an id of "urlhash" so we can find it easily later.

  2. On the server you can use this value if you need to do something with it. You can even change it if you need to.

  3. On page load on the client, check the value of this this hidden field. You will want to find it by the DIV it is contained in as the auto-generated ID won't be known. Yes, you could do some trickery here with .ClientID but we found it simpler to just use the wrapper DIV as it allows all this Javascript to live in an external file and be used in a generic fashion.

  4. If the hidden input field has a valid value, set that as the URL hash (window.location.hash again) and/or perform other actions.

We used jQuery to simplify the selecting of the field, etc ... all in all it ends up being a few jQuery calls, one to save the value, and another to restore it.

Before submit:

$("form").submit(function() {
  $("input", "#urlhash").val(window.location.hash);
});

On page load:

var hashVal = $("input", "#urlhash").val();
if (IsHashValid(hashVal)) {
  window.location.hash = hashVal;
}

IsHashValid() can check for "undefined" or other things you don't want to handle.

Also, make sure you use $(document).ready() appropriately, of course.

Dobb answered 18/10, 2009 at 23:14 Comment(3)
Great solution, but what's about GET request?Chantell
@Dobb - But how form-submit event gets called when you simply paste the URL in a different browser (because it's just a GET request)?Lippold
@Warlock, regardless of get/post it will work since you are storing the hash in a hidden field.Glottochronology
R
87

[RFC 2396][1] section 4.1:

When a URI reference is used to perform a retrieval action on the identified resource, the optional fragment identifier, separated from the URI by a crosshatch ("#") character, consists of additional reference information to be interpreted by the user agent after the retrieval action has been successfully completed. As such, it is not part of a URI, but is often used in conjunction with a URI.

(emphasis added) [1]: https://www.rfc-editor.org/rfc/rfc2396#section-4

Rosaliarosalie answered 25/11, 2008 at 19:27 Comment(1)
I'm surprised. I have read a lot about SPA and didn't known that. So the browser sends so much sensitive information but not the hash?? I think it should into the future .. at least as a separate HTTP header. This is related: onebigfluke.com/2015/01/…Conker
M
44

That's because the browser doesn't transmit that part to the server, sorry.

Mandiemandingo answered 25/11, 2008 at 15:45 Comment(0)
F
7

Probably the only choice is to read it on the client side and transfer it manually to the server (GET/POST/AJAX). Regards Artur

You may see also how to play with back button and browser history at Malcan

Footstalk answered 30/12, 2008 at 11:16 Comment(0)
T
3

Just to rule out the possibility you aren't actually trying to see the fragment on a GET/POST and actually want to know how to access that part of a URI object you have within your server-side code, it is under Uri.Fragment (MSDN docs).

Theologue answered 30/12, 2008 at 15:13 Comment(1)
IE8, Chrome and Firefox all won't send the hash to the server; so, the Uri.Fragment is always an empty string if you examine Request.Url.Fragment server-side (as per the replies above.)Toilet
M
0

Possible solution for GET requests:

New Link format: http://example.com/yourDirectory?hash=video01

Call this function toward top of controller or http://example.com/yourDirectory/index.php:

function redirect()
{
    if (!empty($_GET['hash'])) {
        /** Sanitize & Validate $_GET['hash']
               If valid return string
               If invalid: return empty or false
        ******************************************************/
        $validHash = sanitizeAndValidateHashFunction($_GET['hash']);
        if (!empty($validHash)) {
            $url = './#' . $validHash;
        } else {
            $url = '/your404page.php';
        }
        header("Location: $url");
    }
}
Margarito answered 9/3, 2016 at 21:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.