Event.ClientX is not working in firefox and chrome - javascript

I have coding which is working in IE but not in firefox and chrome...
function handleWindowClose() {
if ((window.event.clientX < 0) || (window.event.clientY < 0))
{
event.returnValue = "Are You sure to leave this page";
}
}
window.onbeforeunload = handleWindowClose;
Can anyone help me...

window.event is a IE-only thing.
To get it to work in other browsers you have to get the event as an argument of the handler function:
function handleWindowClose(e) {
e = window.event || e;
if ((e.clientX < 0) || (e.clientY < 0))
{
e.returnValue = "Are You sure to leave this page";
}
}
window.onbeforeunload = handleWindowClose;

maybe just add mousemove handler that will store mouse position in variable
var mouse;
function storeMouse(e)
{
if(!e) e = window.event;
mouse = {clientX: e.clientX, clientX:e.clientY};
}
function test(e){
alert(mouse.clientX);
}
and use jquery?
$(window).bind('beforeunload', function() {
if (iWantTo) {
return 'Are You sure to leave this page';
}
});

Related

Fullcalendar mousewheel event prev next

i'm trying to do a simple think in Fullcalendar ( 1.6.2 ) and is to simulate the prev and next button throught the mouse wheel up and down, similar to google calendar.
Here is the code i'm using, this code is from another question in here i think, but i can´t remember wich one :S
calendar.bind('mousewheel', function(event, delta) {
var view = calendar.fullCalendar('getView');
//alert(view.name); //Can retrieve the view name successfully
//alert(delta); // Undefined
//alert(event); // [Object object]
if (view.name == "month") {
if (delta > 0) {
alert(delta);
calendar.fullCalendar('prev');
}
if (delta < 0) {
alert(delta);
calendar.fullCalendar('next');
}
return false;
}
});
The problem is delta is Undefined
Anyone have a clue what i'm doing wrong? I'm very new to Jquery or Javascript
EDIT
var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel";
// left: 37, up: 38, right: 39, down: 40,
// spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
var keys = [37, 38, 39, 40];
function preventDefault(e) {
e = e || window.event;
if (e.preventDefault)
e.preventDefault();
e.returnValue = false;
}
function keydown(e)
{
for (var i = keys.length; i--;) {
if (e.keyCode === keys[i]) {
preventDefault(e);
return;
}
}
}
function wheel(e) {
preventDefault(e);
}
function disable_scroll()
{
if (window.addEventListener) {
window.addEventListener('DOMMouseScroll', wheel, false);
}
window.onmousewheel = document.onmousewheel = wheel;
document.onkeydown = keydown;
}
function enable_scroll()
{
if (window.removeEventListener) {
window.removeEventListener('DOMMouseScroll', wheel, false);
}
window.onmousewheel = document.onmousewheel = document.onkeydown = null;
}
calendar.bind(mousewheelevt, function(e)
{
var evt=window.event || e;
var delta=evt.detail ? evt.detail*(-120) : evt.wheelDelta;
if(delta > 0){
calendar.fullCalendar('next');
}
if(delta < 0){
calendar.fullCalendar('prev');
}
});
calendar.bind("mouseleave", function()
{
enable_scroll();
});
calendar.bind("mouseenter", function()
{
disable_scroll();
});
Most of this code was copied from the net i have just adapt it to my problem.
This works in Chrome ( latest version ) and I.E ( lastest version )
Doesn´t work in Firefox ( lastest version )
Didn´t check in old versions of any of them.
Can anyone see the problem of not working in FF?
I think i got it, its a bit hacky but it does the trick!!! Any constructive critics are welcome. This is now working in IE, Firefox, Chrome recent versions.
//This checks the browser in use and populates da var accordingly with the browser
var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel";
//Prevents the scroll event for the windows so you cant scroll the window
function preventDefault(e) {
e = e || window.event;
if (e.preventDefault)
e.preventDefault();
e.returnValue = false;
}
//I think this could be eliminated but in the examples i found used it
function wheel(e) {
preventDefault(e);
}
//adds the scroll event to the window
function disable_scroll(){
if (document.attachEvent) //if IE (and Opera depending on user setting)
document.attachEvent("on"+mousewheelevt, wheel);
else if (document.addEventListener) //WC3 browsers
document.addEventListener(mousewheelevt, wheel, false);
}
//removes the scroll event to the window
function enable_scroll()
{
if (document.removeEvent) //if IE (and Opera depending on user setting)
document.removeEvent("on"+mousewheelevt, wheel);
else if (document.removeEventListener) //WC3 browsers
document.removeEventListener(mousewheelevt, wheel, false);
}
//binds the scroll event to the calendar's DIV you have made
calendar.bind(mousewheelevt, function(e){
var evt = window.event || e; //window.event para Chrome e IE || 'e' para FF
var delta;
delta = evt.detail ? evt.detail*(-120) : evt.wheelDelta;
if(mousewheelevt === "DOMMouseScroll"){
delta = evt.originalEvent.detail ? evt.originalEvent.detail*(-120) : evt.wheelDelta;
}
if(delta > 0){
calendar.fullCalendar('next');
}
if(delta < 0){
calendar.fullCalendar('prev');
}
});
//hover event to disable or enable the window scroll
calendar.hover(
function(){
enable_scroll();
console.log("mouseEnter");
},
function(){
disable_scroll();
console.log("mouseLeave");
}
);
//binds to the calendar's div the mouseleave event
calendar.bind("mouseleave", function()
{
enable_scroll();
});
//binds to the calendar's div the mouseenter event
calendar.bind("mouseenter", function()
{
disable_scroll();
});

onkeydown and onkeyup events don't work on Internet Explorer 8

My code works without problems on IE9/IE10, FF, Chrome and opera but on older Internet Explorer no Keyboard input is handled.
I have the following code for handling events. It should only fire when a new button is pressed.
lastEvent = void 0;
heldKeys = {};
window.onkeydown = function(event) {
if (lastEvent && lastEvent.keyCode === event.keyCode) {
return;
}
lastEvent = event;
heldKeys[event.keyCode] = true;
switch (event.which) {
case 80:
return myamp.userInput("positiv");
case 81:
return myamp.userInput("negativ");
}
};
window.onkeyup = function(event) {
lastEvent = null;
return delete heldKeys[event.keyCode];
};
You need to bind to the document, not window.
window.onkeyup = function(event) {
window.onkeydown = function(event) {
needs to be
document.onkeyup = function(event) {
document.onkeydown = function(event) {
Try
lastEvent = void 0;
heldKeys = {};
window.onkeydown = function(event) {
event = event || window.event; //IE does not pass the event object
if (lastEvent && lastEvent.keyCode === event.keyCode) {
return;
}
lastEvent = event;
heldKeys[event.keyCode] = true;
var keyCode = event.which || event.keyCode; //key property also different
switch (keyCode) {
case 80:
return myamp.userInput("positiv");
case 81:
return myamp.userInput("negativ");
}
};
window.onkeyup = function(event) {
event = event || window.event;
lastEvent = null;
return delete heldKeys[event.keyCode];
};
You have to use normalized key code. Like this:
var keyCode = event.which || event.keyCode;
I tried different solutions but these still did not work correctly in IE8. What I came up with that worked is this:
window.onkeyup = function(e) {
e = (e) ? e : window.event; // check if e is defined
var kc = (e) ? e.which : e.keyCode; // assign keyCode
key = (key === undefined) ? e.keyCode : kc; // if keyCode still undefined, reassign it
if (kc == 13) {
//enter was pressed
}
// other code
}

Disable F5 Ctr+F5 and Ctrl+R in IE8 while input box is focused

I want to disable web page refresh while text box is focused
the code I'm using is this which works great for other shortcuts
except that:
BTW I want to achieve this by plain JavaScript no JQuery and no JavaScript
Frameworks.
document.onkeydown = keyDown //control chars
document.onkeypress = keyDown //any char/number
function keyDown(e) {
//74=j, 75=k, 78=n, 84=t
var forbiddenCtrlKeys = new Array(74,75,78, 84);
//37=left_arrow, 39=right_arrow
var forbiddenAltKeys = new Array(37,39);
//8=backspace, 116=F5
var forbiddenSingleKeys = new Array(8,116);
//per le textbox 116=F5
var forbiddenTextBoxKeys = new Array(116);
key = window.event.keyCode; //IE
if(window.event.ctrlKey){ //ctrl
isCtrl = true;
isAlt = false
checkVal(forbiddenCtrlKeys,window.event.keyCode);
}
else if(window.event.altKey){ //alt
isCtrl = false;
isAlt = true;
checkVal(forbiddenAltKeys,window.event.keyCode);
}
else{ //other
isCtrl = false;
isAlt = false;
var element = e.target.nodeName.toLowerCase();
var curr = document.activeElement
if(curr.tagName=="INPUT")
alert(curr.tagName);
if((element != 'input' && element != 'textarea') || $(e.target).attr("readonly")){
alert(window.event.keyCode);
checkVal(forbiddenSingleKeys,window.event.keyCode);
}
if(document.event.keyCode==116){
alert(window.event.keyCode);
document.event.keyCode=0;
checkVal(forbiddenTextBoxKeys,window.event.keyCode);
}
}
}
function checkVal(set,key){
for(i in set)
{
//case-insensitive comparison
if(String.fromCharCode(set[i]).toLowerCase() == String.fromCharCode(key).toLowerCase())
{
alert('This key combination has been disabled.');
window.event.returnValue = false;
window.event.keyCode = 0;
return true;
}
}
}
So what I've tried to achieve in the else case is whenever I try to press F5 disable it
but it doesn't works, any insight would be highly appreciated

Mousewheel event detection not currently working in Firefox

For some reason I'm having trouble trying to recognize the mousewheel event in Firefox. This is working in IE, Chrome, Safari, Opera but not FF. I am attaching an event listener on DOMMouseScroll, which should be recognized in FF.
Fiddle demo
$(document).unbind('mousewheel DOMMouseScroll').on('mousewheel DOMMouseScroll', function(e) {
var evt = event || e || window.event;
var delta = evt.detail < 0 || evt.wheelDelta > 0 ? 1 : -1;
if (delta < 0) {
// scroll down
} else {
// scroll up
}
});
Your code generates an error in the console. The line:
var evt = event || e || window.event;
is incorrect; there's no "event" variable in scope. You can just use "e" directly. The jQuery code will make sure your handler gets the event parameter as a parameter. Or:
$(document).unbind('mousewheel DOMMouseScroll').on('mousewheel DOMMouseScroll', function(evt) {
var delta = evt.detail < 0 || evt.wheelDelta > 0 ? 1 : -1;
if (delta < 0) {
// scroll down
} else {
// scroll up
}
});
This code save my life.. Works in Chrome, Firefox, Edge, Internet Explorer, Opera...
window.addEventListener('wheel', function(event){
if(event.deltaY < 0){
// wheeled up
}
else {
// wheeled down
}
});

Javascript on second keypress

I've been wondering if there was a simple way to detect if a user presses the same character on the keyboard twice within one second. I've written some code that kind of works but it's unreliable.
var escapeCount = 0;
function reset() {
escapeCount = 0;
setTimeout('reset();', 1000);
}
window.onload = function() {
reset();
};
document.onkeyup = function(e) {
if (!e) var e = window.event;
var code = e.keyCode ? e.keyCode : e.which;
if (code == 27) escapeCount +=1;
if (escapeCount == 2) {
// stuff on second escape
}
};
Is there a better way to do this? Thanks
It would make sense to reset after 1 second has passed since the last character was pressed. Example:
var lastChar = -1;
document.onkeyup = function(e) {
if (!e) var e = window.event;
var code = e.keyCode ? e.keyCode : e.which;
if (lastChar == code) {
// Same key was pressed twice in a row within 1 second.
} else {
lastChar = code;
setTimeout(function() {lastChar = -1;}, 1000);
}
};
Your timer resets every second, so you not only have to press Escape again within a second of the last Escape, but that also has to have no timeout in between the presses.
It's probably easier to forget the timeout and just remember the time of the last keypress instead:
var lastescapetime= null;
document.onkeyup= function(event) {
if (event===undefined) event= window.event;
if (event.keyCode===27) {
var now= new Date().getTime();
if (lastescapetime!==null && now<lastescapetime+1000) {
alert('You double-escaped!');
lastescapetime= null;
} else {
lastescapetime= now;
}
} else {
lastescapetime= null;
}
};

Categories

Resources