I'm working on SSO page, In there there is function that navigated to another domain single page application which uses angularjs. If users decide to go back, Firefox back button, doesn't navigating to that page.
This should get you started... I disable them in my app, but once they're trapped, you can do with the keypresses what you will.
$(document).ready(function(e) {
// Stop enter/backspace doing their browser defaults.
if (typeof window.event != 'undefined') { // IE
document.onkeydown = function() {
var t=event.srcElement.type;
var kc=event.keyCode;
return ((kc != 8 && kc != 13) || ( t == 'text' && kc != 13 ) || (t == 'textarea') || ( t == 'password' ) || ( t == 'search' ) || ( t == 'submit' && kc == 13));
}
} else {
document.onkeypress = function(e) { // FireFox/Others
var kc=e.keyCode;
if ((kc != 8 && kc != 13) || ( t == 'text' && kc != 13 ) || (t == 'textarea') || ( t == 'password' ) || ( t == 'search' ) || ( t == 'submit' && kc == 13)) {
return true;
} else {
alert('Sorry Backspace/Enter is not allowed here'); // Demo code
return false;
}
}
}
});
Related
I want to create a webpage that listens to keypress event for the body to get what the user is typing including backspace for removing the last letter that the user has typed.
So how can I disable the default behaviour of the backspace button (go back) in the browser to achieve that ?
You can disable backspace with keydown event:
$('html').bind('keydown', function (e) {
if (e.keyCode == 8) {
return false;
}
});
i hope this will work for you .
$(document).unbind('keydown').bind('keydown', function (event) {
var doPrevent = false;
if (event.keyCode === 8) {
var d = event.srcElement || event.target;
if ((d.tagName.toUpperCase() === 'INPUT' &&
(
d.type.toUpperCase() === 'TEXT' ||
d.type.toUpperCase() === 'PASSWORD' ||
d.type.toUpperCase() === 'FILE' ||
d.type.toUpperCase() === 'SEARCH' ||
d.type.toUpperCase() === 'EMAIL' ||
d.type.toUpperCase() === 'NUMBER' ||
d.type.toUpperCase() === 'DATE' )
) ||
d.tagName.toUpperCase() === 'TEXTAREA') {
doPrevent = d.readOnly || d.disabled;
}
else {
doPrevent = true;
}
}
if (doPrevent) {
event.preventDefault();
}
});
How to check if iframe content is empty then then disable "enter key" and "space"
else if Enable.
DEMO HERE
var iframeContainerVal = $("#textEditor").contents().find("body").text();
var container = document.getElementById("textEditor").contentWindow;
function checkPress(){
alert('In Fn')
$(container).keypress(function(event){
if (event.keyCode == 10 || event.keyCode == 13 || event.keycode == 32 ) {
alert('aaa');
event.preventDefault();
}
});
}
if(iframeContainerVal == true) {
checkPress()
}
else {
alert('false')
}
Use return false;
function checkPress(){
alert('In Fn')
$(container).keypress(function(event){
if (event.keyCode == 10 || event.keyCode == 13 || event.keyCode == 32 ) {
return false;
}
});
}
you used event.keycode == 32 in your If statement But, it should capital C in keycode like
event.keyCode == 32
Check for iframe empty content , iframe id = 'if'
if ($('#iframe').contents().find('body').children().length == 0) {
alert('I frame is empty');
if (event.keyCode == 32 || event.keyCode == 13) {
return false;
}
}
I am doing a calculator program using jquery program. In the following code I need to capture the delete key.
$("body").on("keydown",function(evt) {
var ChCod = (evt.which) ? evt.which : event.keyCode
strvalue=String.fromCharCode(ChCod);
if (ChCod > 31 && (ChCod < 48 || ChCod > 57) && (ChCod!=43) && (ChCod!=45) && (ChCod!=42) && (ChCod!=47) && (ChCod!=61) && (ChCod!=13) && (ChCod!=46)) {
return false;
}
else
{
if(ChCod==43 || ChCod==45 || ChCod==42 || ChCod==47 || ChCod==61 || ChCod==13|| ChCod==46)
{
if(ChCod==46)
Dot();
else if(strvalue=='=' || ChCod==13)
Calculate();
else
Operate(strvalue);
}
else
AddDigit(strvalue);
return false;
}
});
If I am using keydown event, It will not correctly trigger the number. (eg: when I pressing the '1' it return 'a')
$("body").on("keyup",function(evt) {
Use key up event then you will get correct char value
I have the following javascript on my page:
var isDirty = false;
function OnTextBoxValueChanged(sender, args)
{
isDirty = true;
}
window.onbeforeunload = function ()
{
if (isDirty)
{
return 'You have unsaved changes on the form.';
}
}
OnTextBoxValueChanged is the handler for RadTextBox's corresponding client event.
if user changes text in textbox, then moves focus to any other element and then presses 'close tab' in browser - confirmation window appears. This is correct. But the problem appears when user changes text and then immediately presses 'close tab'. In this case onbeforeunload event fires before onvaluechanged and isDirty variable has incorrect value (false) in onbeforeunload handler.
Am I doing something wrong or is there a workaround for my case?
It seems like that the only workaround in my case is usage of KeyPress event and filtering system keys (tab, enter etc.)
function OnKeyPress(sender, args)
{
if (!IsSysKey(sender, args))
{
isDirty = true;
}
}
function IsSysKey(sender, args)
{
var kc = args.get_keyCode();
return ((kc == 9) || (kc == 13) || (kc == 19) || (kc == 27) ||
(kc == 45) ||
(kc >= 33 && kc <= 40) ||
(kc >= 91 && kc <= 93) ||
(kc >= 112 && kc <= 123) ||
((kc == 8) && (sender.get_caretPosition() == 0) &&
(sender.get_caretPosition()[1] == undefined)) ||
((kc == 46) && (sender.get_caretPosition() == sender.get_value().length) &&
(sender.get_caretPosition()[1] == undefined)));
}
im using javascript to validate keys in textbox. it is not working :(
function numeric(e) {
return ((e.keyCode == 8) ||
(e.keyCode == 9) ||
(e.keyCode > 47 && e.keyCode < 58) ||
(e.keyCode > 36 && e.keyCode < 41) ||
(e.keyCode == 46) ||
(e.keyCode > 95 && e.keyCode < 106) ||
e.keyCode == 190 ||
e.keyCode == 110);
}
help me...
function numeric(e) {
e = e || window.event;
keycode = e.keyCode || e.which;
if(keycode === 13){
alert("cheese");
}
}
I know that in I.E. you can set event.keyCode=0 to suppress the key appearing in the control. But I think you need to trap the onkeydown. Firefox might have an equivalent. This is good because it prevents the key actually "arriving" at the control.
Also keep in mind that you might need to handle combinations of Shift + key and alt + key.
a good debug technique for this sort of thing is to say windows.status = event.keyCode,
and you can see what the keycode is as you type it...
Just try out the following code. I have checked F5 keycode, you can check as you want
function disableKey(event)
{
if (!event) event = window.event;
if (!event) return;
var keyCode = event.keyCode ? event.keyCode : event.charCode;
if (keyCode == 116) {
showMsg("This functionality is disabled.");
window.status = "F5 key detected! Attempting to disabling default response.";
window.setTimeout("window.status='';", 2000);
// Standard DOM (Mozilla):
if (event.preventDefault) event.preventDefault();
//IE (exclude Opera with !event.preventDefault):
if (document.all && event && !event.preventDefault) {
event.cancelBubble = true;
event.returnValue = false;
event.keyCode = 0;
}
return false;
}
}
function setEventListenerForFrame(eventListener)
{
document.getElementById('your_textbox').onkeydown = eventListener;
//frames['frame'].document.onkeypress = eventListener;
}
<body onload="setEventListener(disableKey);">
Try this if you want a numbers only textbox:
function numbercheck(event) {
var unicode = event.charCode; var unicode1 = event.keyCode; if (navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Safari") != -1) {
if (unicode1 != 8) {
if ((unicode >= 48 && unicode <= 57) || unicode1 == 37 || unicode1 == 39 || unicode1 == 35 || unicode1 == 36 || unicode1 == 9 || unicode1 == 46)
{ return true; }
else
{ return false; }
}
}
if (navigator.userAgent.indexOf("MSIE") != -1 || navigator.userAgent.indexOf("Opera") == -1) {
if (unicode1 != 8) {
if (unicode1 >= 48 && unicode1 <= 57)
{ return true; }
else
{ return false; }
}
}
}
And in your textbox call it on the onkeypress event:
onkeypress="return numbercheck(event)"