Response.Redirect with a fragment identifier causes unexpected refresh when later using location.hash

萝らか妹 提交于 2019-11-29 07:47:43
EricLaw

This will happen in IE if there's a directive that forbids caching on the post-redirection page. If you remove the No-Cache header, you will find that the problem no longer repros.

Here's a Meddler script that demonstrates the behavior:

import Meddler;
import System;
import System.Net.Sockets;
import System.Windows.Forms;

class Handlers
{
    static function OnConnection(oSession: Session)
    {
        if (oSession.ReadRequest())
        {       
            var oHeaders: ResponseHeaders = new ResponseHeaders();
            oHeaders["Connection"] = "close";

            if (oSession.urlContains("postRedir.asp"))
            {
                oHeaders.Status = "200 OK";
                oHeaders["Content-Type"] = "text/html"; 
                oHeaders["Cache-Control"] = "no-cache";
                oSession.WriteString(oHeaders);
                oSession.WriteString("");
                oSession.WriteString("Top of page<br /><br /><br /><br /><br /><br />");
                oSession.WriteString("<br /><br /><br /><br /><div id='firstAnchor'>Div with ID firstAnchor</div>Click this link: <A href='#secondAnchor'>#secondAnchor</A>");
                oSession.WriteString("<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />");
                oSession.WriteString("<br /><br /><br /><br /><br /><br /><br /><br /><br /><div id='secondAnchor'>Div with ID secondAnchor</a>");

            }
            else
                if (oSession.urlContains("doRedir.asp"))
                {
                    oHeaders.Status = "301 Redir";
                    oHeaders["Location"] = "/postRedir.asp#firstAnchor";                        
                    oSession.WriteString(oHeaders.ToString());          
                }
            else
            {
                oHeaders.Status = "200 OK";
                oHeaders["Content-Type"] = "text/html"; 
                oSession.WriteString(oHeaders);
                oSession.WriteString("This is a test case for http://stackoverflow.com/questions/1985056/response-redirect-with-a-fragment-identifier-causes-unexpected-refresh-when-later<BR/>");
                oSession.WriteString("Click this link: <A href='doRedir.asp'>Do Redirect</A>");
                oSession.WriteString("<form action='doRedir.asp' method='post'><input type=submit value='Submit as form' /></form>");
            }
        }

        oSession.CloseSocket();
    }
}
Erik Morén

I have the same problem. I have tried Eric's suggestion above (without the Cache-Control header), and it still does not work. This seems to be a bug in IE for sure.

This question mentions a bug with these words: "So this bug may be is the ie bug .when web app via a redirect,the browser may only see the prev url ,so when you modify the location.hash ,the browser do the url change ,so refresh the page."

I have also tried the solution suggested there with different types of charsets, but with no success.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!