设置“location.hash”无法在 IE 中使用部分页面回发。它适用于火狐浏览器

发布于 2024-09-07 02:26:24 字数 1839 浏览 7 评论 0原文

我有一个奇怪的问题,我找不到解决方案。为了澄清我的问题,我做了一个简单的测试页面:

    <%@ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沉默的熊 2024-09-14 02:26:24

我找到了解决方案。如此博客所示。

页面上发生以下情况:

为了使用 ASP.NET 页面上的更新面板进行部分回发,必须在页面上嵌入脚本管理器。此脚本管理器控件将在页面上呈现大量 Javascript,UpdatePanel 等控件将利用这些控件对页面进行时髦的部分更新。

这个 Javascript 篡改了 url 的哈希值,似乎是为了在使用更新面板时启用历史记录和深层链接,并且由于某些(对我来说仍然未知)原因,它更改了 url,删除了哈希标记之后的所有内容。

他提供的第一个解决方案对我有用。您需要在 scriptmanager 声明之后向 ASP.NET 页面添加类似的行:

<script type=”text/javascript”>
Sys._Application.prototype._setState = function() {}
</script>

I found a solution. As seen on this blog.

The following stuff happens on the page :

In order to make a partial postback using the Update Panel on an ASP.NET page, one has to embed a Script Manager on the page. This Script Manager control will render a large amount of Javascript on the page, that controls like the UpdatePanel will utilize to make the snazzy partial update of the page.

This Javascript tampers with the url’s hash value, appearently to enable history and deeplinking when using Update Panels, and for some (to me still unknown) reason it changes the url, removing everything after the hash mark.

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 :

<script type=”text/javascript”>
Sys._Application.prototype._setState = function() {}
</script>
琉璃梦幻 2024-09-14 02:26:24

尝试使用 0 以外的其他内容,例如:

window.location.hash = "#a";

我认为 #0 意味着滚动到名称为 0 的标签,并且默认情况下至少在 IE 非标准中不允许这样做:P

try to use something else than 0, for example :

window.location.hash = "#a";

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

栖竹 2024-09-14 02:26:24

除了使用 window.location.hash 之外,您还可以使用 href:

window.location.href = '#0';

这适用于所有浏览器,并且可以应用于每个网页。

Instead of using window.location.hash, you also could use the href:

window.location.href = '#0';

This works in all browsers and it could be applied on every webpage.

゛清羽墨安 2024-09-14 02:26:24

我遇到了一个模拟问题,我监听 hasg 更改,并且 Scriptmanager 在第一次回发时不断重置哈希值。通过重写 _setState 函数的 Pascals 解决方案对我来说不起作用,但我找到了一种可能对某人有所帮助的不同方法。

$(window).hashchange(function () {
   if (window.location.hash == '#' || window.location.hash == '') {
      window.history.back(-1);
   }
});

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.

$(window).hashchange(function () {
   if (window.location.hash == '#' || window.location.hash == '') {
      window.history.back(-1);
   }
});

hashchange is a jQuery-plugin I found here

长安忆 2024-09-14 02:26:24

非常感谢 Pascal 在这方面的贡献。他的解决方案奏效了。我把它直接放在 scriptmanager 标签后面。当 URL 中包含哈希值时,我在所有浏览器中看到了登陆该页面时的行为。

我的页面使用一个函数通过 .animate 滚动到特定的哈希位置,并且在 Pascal 的修复下仍然可以很好地运行:

    function goToByScroll(id) {
        $('html,body').animate({ scrollTop: $("#" + id).offset().top +160 }, 'slow');
    }

    if (window.location.hash != '') {
        goToByScroll(window.location.hash.substr(1));
    };

据我所知,页面上的任何其他功能都没有受到损害。我已经在 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:

    function goToByScroll(id) {
        $('html,body').animate({ scrollTop: $("#" + id).offset().top +160 }, 'slow');
    }

    if (window.location.hash != '') {
        goToByScroll(window.location.hash.substr(1));
    };

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文