设置“location.hash”无法在 IE 中使用部分页面回发。它适用于火狐浏览器
我有一个奇怪的问题,我找不到解决方案。为了澄清我的问题,我做了一个简单的测试页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test1.aspx.cs" Inherits="MyApplication.Web.Surveillance.Reports.test1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptmanager" runat="server" />
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:Button ID="btn1" runat="server" Text="test1" />
<%= DateTime.Now %>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btn2" runat="server" Text="test2" />
<script type="text/javascript" language="javascript">
$(document).ready(pageInit);
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(pageInit);
function pageInit() {
window.location.hash = "#0";
}
</script>
</form>
</body>
</html>
在这个测试页面中,我有两个按钮。 “test1”按钮位于更新面板内。 “test2”按钮在外面。当我运行此页面时,我可以看到网址,例如“http://localhost/test/test1 .aspx#0”。单击“test1”按钮后,我可以看到网址更改为“http://localhost/test/ test1.aspx#”。漏掉了“0”。之后,一切都OK了。如果我测试“test2”按钮。一切都还好。
我还发现这个问题只发生在IE中。火狐浏览器工作正常。
我的问题是当我单击“test1”按钮时如何保留正确的网址?
谢谢
I have a strange problem to which I could not find a solution. In order to clarify my problem, I did a simply test page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test1.aspx.cs" Inherits="MyApplication.Web.Surveillance.Reports.test1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptmanager" runat="server" />
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:Button ID="btn1" runat="server" Text="test1" />
<%= DateTime.Now %>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btn2" runat="server" Text="test2" />
<script type="text/javascript" language="javascript">
$(document).ready(pageInit);
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(pageInit);
function pageInit() {
window.location.hash = "#0";
}
</script>
</form>
</body>
</html>
In this test page, I have two button. "test1" button is inside an updatepanel. "test2" button is outside. When I run this page, I can see the url, for example "http://localhost/test/test1.aspx#0". After I click the "test1" button, I can see the url changed to "http://localhost/test/test1.aspx#". The "0" is missed. And after that, everything is ok. If I test the "test2" button. Everything is ok too.
I also find this problem only happens in IE. FireFox works fine.
My question is how can I keep the right url when I click the "test1" button?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我找到了解决方案。如此博客所示。
页面上发生以下情况:
他提供的第一个解决方案对我有用。您需要在 scriptmanager 声明之后向 ASP.NET 页面添加类似的行:
I found a solution. As seen on this blog.
The following stuff happens on the page :
The first solution he provided worked for me. You need to add a line like that to your ASP.NET page after the scriptmanager declaration :
尝试使用 0 以外的其他内容,例如:
我认为 #0 意味着滚动到名称为 0 的标签,并且默认情况下至少在 IE 非标准中不允许这样做:P
try to use something else than 0, for example :
I think #0 mean the scroll to the tag that name is 0 and this is not allowed by default at least in the IE none-standard :P
除了使用 window.location.hash 之外,您还可以使用 href:
这适用于所有浏览器,并且可以应用于每个网页。
Instead of using window.location.hash, you also could use the href:
This works in all browsers and it could be applied on every webpage.
我遇到了一个模拟问题,我监听 hasg 更改,并且 Scriptmanager 在第一次回发时不断重置哈希值。通过重写 _setState 函数的 Pascals 解决方案对我来说不起作用,但我找到了一种可能对某人有所帮助的不同方法。
hashchange 是一个 jQuery 插件,我在这里找到
I had a simulair problem, where I listen for hasg changes and Scriptmanager keept reseting the hash on the first postback. Pascals solution by overriding the _setState function didnt work for me, but I figure out a diffrent approch that might help someone.
hashchange is a jQuery-plugin I found here
非常感谢 Pascal 在这方面的贡献。他的解决方案奏效了。我把它直接放在 scriptmanager 标签后面。当 URL 中包含哈希值时,我在所有浏览器中看到了登陆该页面时的行为。
我的页面使用一个函数通过 .animate 滚动到特定的哈希位置,并且在 Pascal 的修复下仍然可以很好地运行:
据我所知,页面上的任何其他功能都没有受到损害。我已经在 Windows 7 上的 IE9、FF 20.0.1、Chrome 26.0.1410.64 和 Opera 12.1 中成功测试了该解决方案。
如果我可以投票给你 Pascal,我会的。
Big thanks to Pascal on this one. His solution worked. I placed it directly after the scriptmanager tag. I was seeing the behaviour in all browsers when landing on the page when the URL had a hash in it.
My page uses a function to scroll to the particular hash location using .animate and this still functions nicely with Pascal's fix:
As far as I can tell there is no detriment to any other functionality on the page. I have tested the solution successfully in IE9, FF 20.0.1, Chrome 26.0.1410.64 and Opera 12.1 all on Windows 7.
If I could vote you up Pascal I would.