September 16, 2011

How to Disable or Hide Back Browser Button using JavaScript

How to Disable or Hide Back Browser Button using JavaScript: 
I have found lot of queries on internet about how to disable back browser button.

After lots of search, I came to a conclusion that back browser button cannot be disabled, however we can tweak it according to our requirement. So, the main work is how to tweak it to fulfill our requirement. For this, first we need to identify the need and scenario of disable back button. 

First Scenario:
Need to disable back button so that user cannot go back and forth through browser back button or only use back buttons created in application.

Solution:
Solution for above solution is to use a JavaScript function called window.open(). 

Let us first understand the definition and usage of above function:
Syntax:
window.open(URL,name,specs,replace)


Use:
The open() method opens in new browser window.



Method Description:



ParameterDescription
URLOptional. Specifies the URL of the page to open. If no URL is specified, a new window with about:blank is opened
nameOptional. Specifies the target attribute or the name of the window. The following values are supported:
  • _blank - URL is loaded into a new window. This is default
  • _parent - URL is loaded into the parent frame
  • _self - URL replaces the current page
  • _top - URL replaces any framesets that may be loaded
  • name - The name of the window
specsOptional. A comma-separated list of items. The following values are supported:

channelmode=yes|no|1|0Whether or not to display the window in theater mode. Default is no. IE only
directories=yes|no|1|0Whether or not to add directory buttons. Default is yes. IE only
fullscreen=yes|no|1|0Whether or not to display the browser in full-screen mode. Default is no. A window in full-screen mode must also be in theater mode. IE only
height=pixelsThe height of the window. Min. value is 100
left=pixelsThe left position of the window
location=yes|no|1|0Whether or not to display the address field. Default is yes
menubar=yes|no|1|0Whether or not to display the menu bar. Default is yes
resizable=yes|no|1|0Whether or not the window is resizable. Default is yes
scrollbars=yes|no|1|0Whether or not to display scroll bars. Default is yes
status=yes|no|1|0Whether or not to add a status bar. Default is yes
titlebar=yes|no|1|0Whether or not to display the title bar. Ignored unless the calling application is an HTML Application or a trusted dialog box. Default is yes
toolbar=yes|no|1|0Whether or not to display the browser toolbar. Default is yes
top=pixelsThe top position of the window. IE only
width=pixelsThe width of the window. Min. value is 100
replaceOptional.Specifies whether the URL creates a new entry or replaces the current entry in the history list. The following values are supported:
  • true - URL replaces the current document in the history list
  • false - URL creates a new entry in the history list



Usage:
Using this method, we can customize browser window. 


Now, how can we use this function to fulfill our requirement: 
In this scenario, I do not want back browser button and menu bar (because it also provides options to back and forward the page in history tab).


so I will write the code as follows:


window.open("url", "mywindow", "toolbar=0, menubar=0");


See the result in screenshot:


In the screen shot you can see that, back browser button has been hide. Now, user cannot use back browser button.

We have fulfill our first scenario to a bit because user still can go back for forward using back space key and Alt-Left Arrow and on right click on page.

I have already posted how can we disable backspace key (Link) and Alt-Left key (Link)events and disable right click on browser (Link).




2 comments:

  1. Thanks for information.I tested.. this may not work for all browsers.. for this we have to go JQuery.. this may help you.. http://aspnettutorialonline.blogspot.com/2012/05/disable-right-click-on-browser-using.html

    ReplyDelete
  2. Hi Nicolas,

    Actually i am using the below code to disable the back button of browser with alert box. It is working fine in IE but i am unable to get the alert box in Chrome of Firefox.
    I tried with many ways but i didnt get anything for that.
    Can you please give me solution for that and do needfull.
    This is my code.
    window.onbeforeunload = onBack;
    function onBack(evt){
    if (evt == undefined){
    evt = window.event;
    }
    if ( (evt.clientX < 0) ||
    (evt.clientY < 0) ||
    (evt.clientX > document.body.clientWidth) ||
    (evt.clientY > document.body.clientHeight))
    {

    /* alert("EVT X: "+evt.clientX);
    alert("EVT Y: "+evt.clientY);
    alert("client width: "+document.body.clientWidth);
    alert("client height: "+document.body.clientHeight); */
    //alert("Unload from browser button press");
    return calibrate(evt);
    }
    return undefined;
    }
    function noBack()
    {
    //window.history.forward();
    }
    function calibrate(evt){
    if(evt.clientX > 140){
    //do nothing
    }else{
    alert("Clicking Back button is Prohibited"+evt.clientX);
    }
    }

    ReplyDelete