Jump to content

code explanation


jimfog

Recommended Posts

Recently I came across with a code like this:

$('#button').click(function(e) {        // Button which will activate our modal            $('#modal').reveal({                // The item which will be opened with reveal                animation: 'fade',              // fade, fadeAndPop, none                animationspeed: 600,            // how fast animtions are                closeonbackgroundclick: true,   // if you click background will modal close?                dismissmodalclass: 'close'      // the class of a button or element that will close an open modal            });

why the function has as a parameter the letter e?

The code is found here:http://webdesign.tutsplus.com/tutorials/htmlcss-tutorials/build-a-popup-modal-window-using-the-jquery-reveal-plugin/?search_index=2

 

Scroll down to step 8. The article refers to the jquery reveal plugin.

Link to comment
Share on other sites

The name of it doesn't matter. Event handlers are passed an event object describing the event. The code will still work if you remove the parameter from the list, it will still get passed an event object but without a name (you would have to access the arguments array to get to it). This is common to all Javascript events, not just jQuery. Different browsers have different standards for how they deal with events. jQuery normalizes those differences so that all event handlers are passed the event object. You can send it to your browser's console if you want to look at the properties of it. It describes things like what kind of event it was, which element it was triggered for, etc.

Link to comment
Share on other sites

as you said it works without it also.

 

So what is the point using it? From your answer I understand(and tell me if I am wrong) that it is needed for cross browser

 

compatibility. Meaning, without it, some browsers may not interpret correctly an event.

 

In my code, so far, I have never used it...

Link to comment
Share on other sites

It is normalized to account for browser differences, so that regardless of how a certain browser handles events, the event handlers in jQuery will all receive the same object (I assume, I didn't look at the jQuery documentation to verify that).

 

 

 

So what is the point using it?

The event object has several properties that a programmer might want to take advantage of when writing reusable extendable code.

 

 

 

that it is needed for cross browser compatibility

It's not "needed" for anything, it's there if you want to use it.

 

 

 

Meaning, without it, some browsers may not interpret correctly an event.

That's not accurate. Browsers know exactly what they are doing. Programmers don't, a programmer that doesn't know much might write code that works fine in whatever browser they are testing in but doesn't work in others. If jQuery normalizes the event object then it will be the same in all browsers. jQuery does not change how browsers work, it makes things easier for programmers. The browsers are still doing the same things.

Link to comment
Share on other sites

as you said it works without it also.

 

right. you are defining a parameter in your event handler callback function, in this case it is expected the type is going to be an event object. If you don't use it within the handler function, then of course there will be no issues with you not defining the parameter in between the ()'s

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...