如何让使用Ajax的网站页面不能前进和后退

由于采用Ajax.Net技术,要求客户不能用浏览器的后退来导航,因为这样会产生不正确的页面.因为有时,结果是Ajax查询返回的,不会保存 住.JQuery是一个好东西,可以很方便的给每个 <A> 加上事件,来重写它的默认的跳转,我们把它改成单击时,调用 Location.replace来完成页面切换.
代码如下:

$(document).ready(function() {
    $("a").each(function() {
        var href = $(this).attr("href");
        if (!href.match(/^javascript\:__doPostBack\(.+?\)$/i)) {
            $(this).click(function() {
                location.replace($(this).attr("href"));
                //alert('ok');
                return false;
            })
        }
//        else {
//            $(this).click(function() {
//                alert('你点击了LinkButton');
//                //return false;
//            })
//        }
    })
});



现在好了,可是我在我项目中,发现一点问题,由于登陆页面采用Form认证,这样登陆后会调用
FormsAuthentication.RedirectFromLoginPage(userName, false);
来 返回到用户最初请求的页面,但这会造成浏览器切换,会使后退按钮可用.看来我们要登陆成功后,采用注册脚本的形式,来完成这个切换:

        protected void btnLogon_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string userName = this.tbUserName.Text.Trim();
                string userPwd = this.tbPassword.Text.Trim();
                if (Membership.ValidateUser(userName, Utility.HashedString(userPwd)))
                {
                    FormsAuthentication.SetAuthCookie(userName, false);
                    //FormsAuthentication.RedirectFromLoginPage(userName, false);
                    ScriptManager.RegisterStartupScript(this.tbUserName, this.GetType(), "success", string.Format("location.replace('{0}');", FormsAuthentication.GetRedirectUrl(userName,false)), true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this.tbUserName, this.GetType(), "loginfailed", "alert('用户名密码错误');", true);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.tbUserName, this.GetType(), "loginfailed", "alert('验证码不正确');", true);
            }
        }
现在好了,从登陆开始,到退出,浏览器都不会产生后退哟.

star65225692 -
共有0个回答