event object prevent default and different browsers
Hi,
I am quite new to javascript/jquery, however I have been programming in C for quite some time.
I am having problems with the events. Everything works as I would like it to in Chrome, however in Firefox I get errors around this. StackOverflow etc seem to have similar questions but without difinitive answers.
Let me explain. I have a page that is to run a jquery function:
- <script>setupFileExplorer();</script>
That in turn, calls a function showOptions which, when the user clicks on *something* it is to pop up a div under the mouse with some options.
- showOptions();
The function showOptions looks like this:
- function showOptions(){
- event.preventDefault();
- $('#options').css( 'position', 'absolute' );
- $('#options').css( 'top', event.pageY );
- $('#options').css( 'left', event.pageX );
- $('#options').css( 'display', 'block' );
- $('#options').show();
- setTimeout(function() { $("#options").fadeOut(); }, 1000);
- }
Now my understanding is event is sort of global once the document is loaded. So at no point am I passing showOptions or any other function for that matter event.
The above all works nicely in chrome - exactly how I intended (and by coincidence developed it in - first lesson, develop in all browsers...).
When I run it in Firefox it laods fine, but when I click on one of the something's to pop up my options div, I get (in the console):
ReferenceError: event is not defined
on line 66, which is:
- event.preventDefault();
in showOptions.
From reading around the event doesnt exist in the DOM the way it does in Chrome, so what is the generic way of doing this? Do I need to set something up in the $(document).ready code to set event to equal the window.event or something (I have tried event = window.event).
This is my first real hiccup, apart from this I am loving JS/jQuery!