admin管理员组文章数量:1414614
I have a jQuery function that submits a form via menu navigation functions:
$(function () {
$('#sidebarmenu1 a').on('click', function () {
var url = $(this).attr('href');
$('#myform').attr('action', url);
$("#myform").submit();
if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; }
})
});
This section:
if (event.preventDefault)
{ event.preventDefault(); }
else
{ event.returnValue = false; }
Prevents the default action of the sidebar button (I think - still new to this) i.e. to simply navigate to a page.
It is written in this way to keep IE happy, because preventDefault
isn't defined for IE (might be using incorrect terminology there, but IE doesn't like preventDefault
.)
However now this throws up an error in Firefox, because (as I read on other Stack questions) event is not globally defined for Firefox! I get the following error:
ReferenceError: event is not defined
Now according to this Stack question: event is not defined in FireFox, but ok in Chrome and IE
In IE and Chrome, event is resolving to window.event. Firefox doesn't have this property and instead provides an event to an event handler function by passing it as a parameter. jQuery abstracts this difference for you by providing an event object parameter in all browsers.
But I thought I was using jQuery here and am still getting the issue.
Sorry if I'm making basic mistakes, self teaching myself js and jQ. Any help much appreciated.
I have a jQuery function that submits a form via menu navigation functions:
$(function () {
$('#sidebarmenu1 a').on('click', function () {
var url = $(this).attr('href');
$('#myform').attr('action', url);
$("#myform").submit();
if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; }
})
});
This section:
if (event.preventDefault)
{ event.preventDefault(); }
else
{ event.returnValue = false; }
Prevents the default action of the sidebar button (I think - still new to this) i.e. to simply navigate to a page.
It is written in this way to keep IE happy, because preventDefault
isn't defined for IE (might be using incorrect terminology there, but IE doesn't like preventDefault
.)
However now this throws up an error in Firefox, because (as I read on other Stack questions) event is not globally defined for Firefox! I get the following error:
ReferenceError: event is not defined
Now according to this Stack question: event is not defined in FireFox, but ok in Chrome and IE
In IE and Chrome, event is resolving to window.event. Firefox doesn't have this property and instead provides an event to an event handler function by passing it as a parameter. jQuery abstracts this difference for you by providing an event object parameter in all browsers.
But I thought I was using jQuery here and am still getting the issue.
Sorry if I'm making basic mistakes, self teaching myself js and jQ. Any help much appreciated.
Share Improve this question edited Oct 5, 2022 at 18:45 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 30, 2012 at 23:46 GideonGideon 1,8864 gold badges41 silver badges71 bronze badges3 Answers
Reset to default 3This should do..
$(function() {
$('#sidebarmenu1 a').on('click', function(e) {
var evt = e || window.event;
var url = $(this).attr('href');
$('#myform').attr('action', url);
$("#myform").submit();
evt.preventDefault();
})
});
If you use the event object jQuery passes to the event handler you wont have problems
$('#sidebarmenu1 a').on('click', function (event) {
var url = $(this).attr('href');
$('#myform').attr('action', url);
$("#myform").submit();
event.preventDefault();
})
If I had a couple of more points I would up-vote the second answer (passing in the jQuery 'event' argument).
I had unknowingly relied on the global event defined by browsers other than Firefox. I went back through my code and ensured I was always specifying the event parameter on all click events.
E.g.
$('#situationTextInput')
.val('')
.fadeTo(1000, 1.0)
.focus()
.off('keydown')
.on('keydown', function (event) {
VsUtils.handleSpanishTextKeydown(event);
});
I've also gotten into the habit of calling .off prior to .on as most of my code is using single pages, reusing content. Without the .off I was inadvertently stacking up events (even if they were the same callback) in subsequent steps of the dialog.
A side effect of changing my code to pass on 'event' directly to the handler was the binding to 'this.' changed. I chose to refactor the code to change 'this.' references to 'event.currentTarget.'
本文标签: Using JavaScriptjQuery event handler in FirefoxStack Overflow
版权声明:本文标题:Using JavaScriptjQuery event handler in Firefox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745191610a2646936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论