Why Firefox says that window.event is undefined? (call function with added event listener)
try getting the event using the parameter passed (named e
in this case). i tested this and both window.event
and the e
is supported in chrome.
try checking for both, whichever exists
var ex = {
exampl: function(e){
console.log(window.event);
console.log(e);
//check if we have "e" or "window.event" and use them as "evt"
var evt = e || window.event
}
}
Firefox window.event is undefined error
That's because it is. window.event
is for older versions of IE.
The typical way to do this is:
function postBackByObject(e) {
e = e || window.event;
var o = e.srcElement || e.target;
// ...
}
Workaround for Firefox's lack of window.event?
Here's an ugly hack, based on the assumption that all event listeners will ultimately be registered by the EventTarget.addEventListener()
method. The "solution" below changes EventTarget
's prototype (yuck!) so as to wrap all event listeners in a method that will assign the event to window.event
before delegating to the actual listener.
const addEventListener = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function(name, handler) { addEventListener.call(this, name, function(event) { window.event = event; handler(event); })};
function clicked() { // just demonstrating that window.event is set console.log(window.event.target.innerHTML);}
document.getElementById('one').addEventListener('click', clicked);document.getElementById('two').addEventListener('click', clicked);
<button id="one">Test 1</button><button id="two">Test 2</button>
Type error: event is undefined in firefox about javascript
Remove the var
from var event = window.event
. The variable is already declared (as an argument), so re-declaring it with var
can only lead to problems.
To be specific, due to hoisting, here is what your code boils down to:
function MouseDown(event) {
var event; // = undefined
if( !event) { // always true
event = window.event; // undefined in modern browsers
}
X = event.pageX; // ERROR!
}
Without the var
, all is well! event.path is undefined running in Firefox
The path
property of Event
objects is non-standard. The standard equivalent is the composedPath
method. But it was new when the question was asked (2016); it's well-established as of this update in January 2023.
So you may want to try composedPath
and fall back to path
(or just use composedPath
now it's established):
// Written in ES5 for compatibility with browsers that weren't obsolete
// yet when the question was posted, although they are now
var path = event.composedPath ? event.composedPath() : event.path;
if (path) {
// You got some path information
} else {
// This browser doesn't supply path information
}
Obviously that won't give you path information if the browser doesn't supply it, but it allows for both the old way and the new, standard way, and so will do its best cross-browser.Example:
// Written in ES5 for compatibility with browsers that weren't obsolete
// yet when the question was posted, although they are now
document.getElementById("target").addEventListener("click", function (e) {
// Just for demonstration purposes
if (e.path) {
if (e.composedPath) {
console.log("Supports `path` and `composedPath`");
} else {
console.log("Supports `path` but not `composedPath`");
}
} else if (e.composedPath) {
console.log("Supports `composedPath` (but not `path`)");
} else {
console.log("Supports neither `path` nor `composedPath`");
}
// Per the above, get the path if we can, first using the standard
// method if possible, falling back to non-standard `path`
var path = event.composedPath ? event.composedPath() : event.path;
// Show it if we got it
if (path) {
console.log("Path (" + path.length + ")");
Array.prototype.forEach.call(path, function(entry) {
console.log(entry === window ? "window" : entry.nodeName);
});
}
});
.as-console-wrapper {
max-height: 100% !important;
}
<div id="target">Click me</div>
Javascript - e (event) is undefined in firefox
Change your handlers to this...
jQuery('.reallyNice_slider').each(function(){
var slider = new reallyNiceSlider(this);
if(document.addEventListener)
document.addEventListener('mousemove',
function(e){
slider.sliderMove(e, slider)
}, false);
else
document.attachEvent('onmousemove',
function(){
slider.sliderMove(window.event, slider)
});
this.style.visibility = 'visible';
});
Then change your functions to receive the event object as the first argument.this.sliderMove = function(e, obj){
//var e = window.event; // remove this
var range = obj.range;
var offset, cursor={x:0,y:0}, diff={x:0,y:0,total:0,ratio:0}, opacity, max = range.max+obj.safe, child;
var selectedObj = null;
Related Topics
Get the Current Year in JavaScript
I Can't Reference an Image in Next.Js
How to 'Await' on an Rx Observable
Rendering to Js with Jinja Produces Invalid Number Rather Than String
How to Use Promise in Foreach Loop of Array to Populate an Object
Why Are Await and Async Valid Variable Names
How to Handle Oncut, Oncopy, and Onpaste in Jquery
Attaching Click Event to a Jquery Object Not Yet Added to the Dom
Regular Expression for Ip Address Validation
JavaScript Strings Outside of the Bmp
Select a Complete Table with JavaScript (To Be Copied to Clipboard)
Do We Need Semicolon at the End
Angularjs - Cancel Route Change Event
Way to Know If User Clicked Cancel on a JavaScript Onbeforeunload Dialog
Create a <Ul> and Fill It Based on a Passed Array
How to Show a Spinner While Loading an Image via JavaScript
Catching Errors in JavaScript Promises with a First Level Try ... Catch