Invoking browser functions with JavaScript
Browser functionality can be disabled using the JavaScript. JavaScript has the ability to capture and handle the key events. We will look at couple of examples on how to trigger browser functionality using Java script.
Disabling new page event and ctr key event in browser
Following is the example which shows how to disable the ctrl + N (new page event) and back button in the browser.
<html>
<body onkeydown=”return
fnDisableNewPage(event)” >
<p>Test</p>
<script>
/******************************************************************************
*FunctionName :funcDisNewPage()
*Description :This function disables ctrl+key
combinations
*Input Parameters : event
*Returns :true
*****************************************************************************/
function funcDisNewPage(e)
{
var forbiddenKey = ‘N’;
var key;
var isCtrl;
//Check for the event
if( window.event )
{
//IE
key = window.event.keyCode;
//check is Ctrl key is pressed
if( window.event.ctrlKey )
{
isCtrl = true;
}
else
{
isCtrl = false;
}
}
else
{
//firefox
key = e.which;
//check if Ctrl key is pressed
if( e.ctrlKey )
{
isCtrl = true;
}
else
{
isCtrl = false;
}
}
//if ctrl is pressed check if other key is same as
the forbidenKey
if( isCtrl )
{
//case-insensitive comparation
if( forbiddenKey.toLowerCase() ==
String.fromCharCode(key).toLowerCase() )
{
//give an alert if
Ctrl+n is pressed
alert( “You
cannot create a copy of the Current Form by using CTRL + N
“);
return false;
}
}
return true;
}
Disabling back button in browser
/******************************************************************************
*FunctionName :funcDisBack()
*Description :The function disables the back
button of browser
*****************************************************************************/
function funcDisBack()
{
history.go(+1);
}
</script>
</body>
</html>
Following table show the Keyboard keys and event code
mappings.
These key codes are using the JavaScript to disable the
browser functionalities.
|
Key |
ASCII |
Mozilla key codes |
IE key codes |
Opera key codes |
pseudo ASCII codes |
|
|
Alphabetic keys |
97/65 to 122/90 |
ASCII code of uppercase version of the letter |
||||
|
A to Z |
65 to 90 |
|||||
|
Space |
32 |
32 |
32 |
32 |
32 |
|
|
Enter |
13 |
13 |
13 |
13 |
13 |
|
|
Tab |
9 |
9 |
9 |
9 |
9 |
|
|
Esc |
27 |
27 |
27 |
27 |
27 |
|
|
Backspace |
8 |
8 |
8 |
8 |
8 |
|
|
Modifier Keys |
||||||
|
Key |
ASCII |
Mozilla key codes |
IE key codes |
Opera key codes |
pseudo ASCII codes |
|
|
Shift |
- |
16 |
16 |
16 |
16 |
|
|
Control |
- |
17 |
17 |
17 |
17 |
|
|
Alt |
- |
18 |
18 |
18 |
18 |
|
|
Caps Lock |
- |
20 |
20 |
20 |
20 |
|
|
Num Lock |
- |
144 |
144 |
144 |
144 |
|









This is not abusive. Customer requirements.
Why would you do this? Seems user abusive.