JavaScript和浏览器';s后退/前进按钮
我有一些JavaScript,如果ASP.NET的JavaScript和浏览器';s后退/前进按钮,javascript,asp.net,http,browser,cross-browser,Javascript,Asp.net,Http,Browser,Cross Browser,我有一些JavaScript,如果ASP.NET的会话中存在值,它会显示一个通知。这可能不是我现在学习的最佳解决方案,但下面是代码: protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (Session["Alert"] != null) { Page.ClientScript.RegisterStartupScr
会话中存在值,它会显示一个通知。这可能不是我现在学习的最佳解决方案,但下面是代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["Alert"] != null)
{
Page.ClientScript.RegisterStartupScript(GetType(), "alert", Session["Alert"].ToString());
Session["Alert"] = null;
}
}
}
会话[“警报”]
具有显示通知的JS功能:
Session["Alert"] = "showAlert('test')";
function showAlert(msg) {
alert(msg);
}
如果为其分配了一个值,则代码将成功运行并显示通知。单击浏览器的“后退”按钮,然后单击“前进”按钮,再次显示警报,因为页面没有经历其生命周期
如何防止警报显示不止一次?我尝试添加一个全局JavaScript变量,如var-alertshow=false编码>并在showart
功能中检查,但这不起作用
有没有一种跨浏览器的方法可以解决这个问题?首先,您必须避免客户端的导航缓存(通常后退/前进实际上不会将页面拉回)
在这一障碍之后,您可以(讽刺地)使用会话变量来存储是否已查看[output],并且只将其转储到页面一次
如果这似乎太多,您可以在函数中添加一个cookie检查,这样当它运行(并重新运行)时,它会检查cookie,如果不存在,则发出警报并设置cookie。伪代码:
function showAlert(msg){
if (cookie[msg] == null){ // a cookie doesn't exist for this msg
alert(msg); // alert the msg
cookie[msg] = true; // set the cookie so the next pass is ignored
}
}
谢谢你的信息。我认为饼干是最好的选择。我将了解如何使用JS获取/设置cookies。