Monday, November 4, 2013

Browser Back Button handling after Sign Out throug JavaScript


Disabling/Handling Browser's back button is a common functionality to be implemented or the problem to be solved during development of web applications now a days.  So when I found this problem in front of my nose, it took my a lot of time to understand and implement it as per my project. This simple Tip/Trick is focused on providing a simple and cross browser compatible solution in this context.

NOTE: This mini article have been published on and it is here to share the knowledge.

Using the code 

JavaScript has become a most commonly used platform to sort out different types of problems on the client side (some time to enhance functionality and some time due to client restriction). In the same way, I have chosen the JavaScript to solve this oldie goldy problem.
To make my work (code) compatible for major browsers, I have extended the script a bit, So that it could work without any error.
I have created a JavaScript function in ASPX page which is being called from the code behind of  SignOut page of my application after the handling of Sessions (i.e., abandoning/clearing the sessions etc.).  
function ClearHistory()  
    //defining variables to get the browser width and height.
    var width;
    var height;

    // getting the Browser name which is currently in use.
    var BrowserName = navigator.appName;

    //handling the IE due to limitations of this browser
    // i.e., failure in getting proper height of browser.
    if (BrowserName == "Microsoft Internet Explorer") {
        //setting up height and width variable values.
        height = document.documentElement.clientHeight;
        width = document.body.clientWidth;
    // code block to be executed in case of other browsers.i.e. except IE.
        height = window.innerHeight;
        width = window.innerWidth;

    // getting the currently opened window. and than closing it to get rid of back button.
    //Mainly this is done to cover up the firefox limitation of window.close()
    var win ="about:blank", "_self");

    //Opening a new Window with the Login page url
    //to redirect the user towards successful signout.
    var newWin ='Login.aspx');

    //formatting the newly opened window to be adjusted accordingly.
    newWin.moveTo(0, 0);
    newWin.resizeTo(width, height);

Points of Interest

While searching for a solution for this problem, I found a lot of solutions posted around, and I have created this by combining parts from there. 
I hope this could be much helpful and easy to implement (even though there is some hotchpotch). Any guidance from the seniors, comments/suggestions/errors are truly welcomed. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL).

Thursday, October 10, 2013

How to download a file on Button Click


In most of the web applications now a days, there comes a need when we have to provide a link to download some sample file or file like that as per the requirements. So this tip is to perform this task in the easiest way (which I have found so far, as I am a newbie.

Using the Code

The following code performs the file download process in the simplest way, either from the application's local folder or from some system directory.
The developer has to place these line of codes inside the buttonclick or imagebutton clickevent.
Response.ContentType = "Application/xlsx";
Response.AppendHeader("Content-Disposition", "attachment; filename=filename.xlsx");

In the first line, we have to mention the content type, I have used it for Excel 2007 files while here, we can use the PDF or image as well.
The second line is just appending the header to the httpResponse and here we have to mention the filename along with the extension which we want to download.
While in the third line Complete Path where file is placed has to be provided.
In the last line of code, the Response is ending.

Points of Interest

Many code snippets are available on Google, but I found this one pretty interesting and straight forward for any programmer who is a kind of newbie like myself.
Do give me your feedback, as this is my first effort on any blog in my life, either suggestions, corrections or praises. :)

This Post is also available here File Download on Button Click  on i.e in fact the birthplace of this tip/trick.