RSS LOGORss   Welcome Guest  Login Register
Welcome to our Blogs
Back Back
Subject


mag
2009/03/18 06:51AM
How to Alert the User and Handle "Back" button of IE      Number of views: 1979

 

 

 

 

Sometimes, you do alert the user with the javascript “alert” function, but, when the user navigate to another page then clicking the “Back” button, triggers the “alert” again, so, how could you disable that?

Below is a class that uses the cookies to save some data along with a key for your message, and it checks up the cookie and determine whether to show the message or not.
 
To use the code:
                Just copy and best the below code in a “cs” file and call this function

                       JSAlertMessage.AlertMessage( this, 'your own custom message');

"this" : a reference to your page (System.Web.UI.Page)

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
///<summary>
/// Message Alert Handler By: Mohamed Gad
/// Description:
///     When you show the user an Alert message then the user navigate to another page, so, if the user pushes
///     the Browser's back button, the Message is shown again, this function disables that and only shows the user
///     the message once.
///     
///     Example
///     JSAlertMessage.AlertMessage( this, 'your own custom message');
///    
///     So, what it does that, it registers some code block that I have used from
///     http://www.quirksmode.org/js/cookies.html
///     along with my custom function that really shows the message, further more, it registers
///     a start up (onload) function for showing your message.
///    
/// Concept:
///     The used concept is cookies, so, I generate a different GUID for each alert and I store the new GUIDs in
///     a cookie (one day cookie :)) and show the alert, but, before showing the alert, the code checks if the same
///     GUID is there and, it won't show the alert for existed GUIDs.
///    
///
/// Final note:
///     The code is provided as is, no reliability of any kind, and if you are going to use this file, please, keep
///     this comments and reference (http://www.microsoftawy.com)
///    
///
/// By:
///     Mohamed Gad
///     18'th of March, 2009
///</summary>
public class JSAlertMessage
{
    public static void AlertMessage(System.Web.UI.Page page, string Message)
    {
        var GuidString = Guid.NewGuid().ToString();
        GuidString = GuidString.Replace("{", "").Replace("}", "").Replace("-", "");
        page.ClientScript.RegisterClientScriptBlock(Type.GetType("System.String"), "MessageAlertScript",
            "<script>function AlertMessage(guid,message){var result=readCookie(guid);if(result==null){alert(message);createCookie(guid,\"1\",1);}}function createCookie(name,value,days){if(days){var date=new Date();date.setTime(date.getTime()+(days * 24 * 60 * 60 * 1000));var expires=\";expires=\"+date.toGMTString();}else var expires=\"\";document.cookie=name+\"=\"+value+expires+\";path=/\";}function readCookie(name){var nameEQ=name+\"=\";var ca=document.cookie.split(';');for(var i=0;i<ca.length;i++){var c=ca[i];while (c.charAt(0) == ' ') c = c.substring(1, c.length);if(c.indexOf(nameEQ)==0)return c.substring(nameEQ.length,c.length);}return null;}function eraseCookie(name){createCookie(name,\"\",-1);}</script>", false);
        page.ClientScript.RegisterStartupScript(Type.GetType("System.String"),
            "MessageAlertString" + GuidString.Substring(26,6), string.Format( "AlertMessage('{0}','{1}');", GuidString, Message), true);
    }
}
 

 

Happy Coding,

Microsoftawy (2009)