如何从服务器端获取地址散列(#) [英] How to get Url Hash (#) from server side

查看:348
本文介绍了如何从服务器端获取地址散列(#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在客户端(JavaScript),可以使用windows.location.hash但无论如何也找不到从服务器端的访问。

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

推荐答案

我们有我们需要坚持跨越ASP.Net后背上的URL哈希的情况。作为浏览器默认不散列发送到服务器,这样做的唯一方法是使用一些JavaScript:

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. 在表单提交,抢哈希( window.location.hash ),并将其存储在一个DIV将这个服务器端的隐藏的输入字段用一个id urlhash ,所以我们可以很容易找到它后。

  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.

在服务器你可以,如果你需要用它做什么使用此值。如果你需要,你甚至可以改变它。

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.

在页面加载的客户端,检查这个这个隐藏字段的值。你会想它包含在为自动生成的ID将不知道DIV找到它。的是的,你可以在这里做一些挂羊头卖狗肉与.ClientID但我们发现它简单的只使用包装DIV,因为它允许所有此Javascript生活在一个外部文件,并在一个通用的方式使用。

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.

如果隐藏的输入字段包含有效的值,设置为URL哈希( window.location.hash再次)和/或执行其他操作。

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

我们使用jQuery来简化场等的选择......这一切的一切它最终被几个jQuery的电话,一个保存价值,而另一个将其还原。

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.

前提交:

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

在页面加载:

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

IsHashValid()可以检查未定义,或者你不想来处理其他事情。

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

此外,请确保您使用 $(文件)。就绪()适当的,当然。

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

这篇关于如何从服务器端获取地址散列(#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆