I've made a page for a client and I initially was working in Chrome and forgot to check if it was working in Firefox. Now, I have a big problem because the whole page is based upon a script that doesn't work in Firefox.
It's based on all "links" that have a rel
that leads to hiding and showing the right page. I don't understand why this isn't working in Firefox.
For instance pages have the id #menuPage
, #aboutPage
and so on. All links have this code:
<a class="menuOption" rel='#homePage' href="#">Velkommen</a>
It's working perfectly in Chrome and Safari.
Here is the code:
$(document).ready(function(){ //Main Navigation $('.menuOption').click(function(){ event.preventDefault(); var categories = $(this).attr('rel'); $('.pages').hide(); $(categories).fadeIn(); }); // HIDES and showes the right starting menu $('.all').hide(); $('.pizza').show(); // Hides and shows using rel tags in the buttons $('.menyCat').click(function(event){ event.preventDefault(); var categori = $(this).attr('rel'); $('.all').hide(); $(categori).fadeIn(); $('html,body').scrollTo(0, categori); }); });
Answer: Execute Code after jQuery Library has Loaded The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading.
To solve the"ReferenceError: document is not defined" error, make sure to only use the document global variable on the browser. The variable relates to the Document Object Model, which represents a web page that is loaded in the browser and can't be used on the server side (e.g. in Node. js).
The ReferenceError object represents an error when a variable that doesn't exist (or hasn't yet been initialized) in the current scope is referenced. ReferenceError is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage() .
This JavaScript exception variable is not defined occurs if there is a non-existent variable that is referenced somewhere. Cause of Error: There is a non-existent variable that is referenced somewhere in the script. That variable has to be declared, or make sure the variable is available in the current script or scope.
You're declaring (some of) your event handlers incorrectly:
$('.menuOption').click(function( event ){ // <---- "event" parameter here event.preventDefault(); var categories = $(this).attr('rel'); $('.pages').hide(); $(categories).fadeIn(); });
You need "event" to be a parameter to the handlers. WebKit follows IE's old behavior of using a global symbol for "event", but Firefox doesn't. When you're using jQuery, that library normalizes the behavior and ensures that your event handlers are passed the event parameter.
edit — to clarify: you have to provide some parameter name; using event
makes it clear what you intend, but you can call it e
or cupcake
or anything else.
Note also that the reason you probably should use the parameter passed in from jQuery instead of the "native" one (in Chrome and IE and Safari) is that that one (the parameter) is a jQuery wrapper around the native event object. The wrapper is what normalizes the event behavior across browsers. If you use the global version, you don't get that.
It is because you forgot to pass in event
into the click
function:
$('.menuOption').on('click', function (e) { // <-- the "e" for event e.preventDefault(); // now it'll work var categories = $(this).attr('rel'); $('.pages').hide(); $(categories).fadeIn(); });
On a side note, e
is more commonly used as opposed to the word event
since Event
is a global variable in most browsers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With