Jump to content

ajax request and ...return false


jimfog

Recommended Posts

Recently I came across with this code in a tutorial found here http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/?search_index=6:

$.ajax({    type: "POST",    url: "bin/process.php",    data: dataString,    success: function() {      //display message back to user here    }  });  return false;

I am trying to understand what return false is doing there.I think that the above ajax request is found in a click event handler...something

not depicted in the tutorial.

Do you think return false is needed there?

Link to comment
Share on other sites

It's explained in the tutorial in step 4

If our script processed successfuly, we can then display a message back to the user, and finally return false so the page does not reload.

It's preventing the default behavior of the form submitting.

Link to comment
Share on other sites

It's explained in the tutorial in step 4

It's preventing the default behavior of the form submitting.

Could we use event.prevent default instead of return false?

What is the possible reason for not using event.prevent default?

Link to comment
Share on other sites

there is one last comment I want to add.

I saw in the examples in stack overflow that in the function/event handler a parameter is passed and that is e...I am talking about this here:

$('a').click(function (e) { 

Do you think there is any problem if someone does not follow the above scheme? For example, this is the code I use:

$('#savecontact').click(function() {   event.preventDefault(); 
Link to comment
Share on other sites

I don't think the version you use is normalized to compensate for browser differences.

So you are saying(in other words) that e is used to compensate for browser differences

Link to comment
Share on other sites

 

 

So you are saying(in other words) that e is used to compensate for browser differences

The event handler will get an event object passed to it regardless of which browser it is. Like Ingolme said, not all browsers use a global object called event.

Link to comment
Share on other sites

I am confused...what should I do?

Link to comment
Share on other sites

I assume, e is the event object...just to be sure.

Link to comment
Share on other sites

I though that jquery could handle browser inconsistencies without resorting to the event object...obviously I was wrong.

 

And something else...should this event object be used only when we want to prevent the default behavior of an event?

What about a keyup event where the user starts typing something...actually I am talking about the case where a username is checked against

the db.

Edited by jimfog
Link to comment
Share on other sites

I though that jquery could handle browser inconsistencies without resorting to the event object...obviously I was wrong.

Making sure that an event object always gets passed to an event handler is one of the ways jQuery normalizes browser differences. Not all browsers will normally pass an event object to an event handler. jQuery makes sure that every event handler gets an event object regardless of which browser the user is using. That is the correct way to do it, that is what the W3C event handling spec outlines. The global window.event object was created by Microsoft when they were fighting against Netscape. The Netscape model was eventually adopted and extended by the W3C. On top of that, the 2 event models also expose different property names to refer to the same things. So, again, jQuery normalizes for that and presents an event object that has the same properties regardless of which browser you're using. So jQuery is trying to get you to do it the right way, but you still want to use the Microsoft way. Do it the right way.

 

If you want to brush up on events in Javascript, I would recommend this:

 

http://www.quirksmode.org/js/introevents.html

 

 

 

And something else...should this event object be used only when we want to prevent the default behavior of an event?

You use the event object any time you want to access or control anything about the event itself. Stopping the default behavior or propagation is not the only purpose of the event object. Go through the articles on Quirksmode if you want to learn about events in Javascript, including the properties and methods of the event object. Events are the foundation of Javascript programming, you need to understand them.

 

 

 

What about a keyup event where the user starts typing something...actually I am talking about the case where a username is checked against the db.

What about it?

Link to comment
Share on other sites

 

What about it?

regrading the keyup event now...here is the code I am using now:

$("#email").keyup(function()

Should I pass an event object as an argument to the above?

From what I have understood so far the answer should be yes.

 

I am asking though, because there is not here any default behavior to be

prevented...it is not a form submit button.

Link to comment
Share on other sites

if you need to access information about the event, then yes, pass it in. Otherwise, it doesn't matter. Note that since you are using jQuery, and as has been explained, the event object you get is the one promised according the jQuery specs.

http://api.jquery.com/category/events/event-object/

Edited by thescientist
Link to comment
Share on other sites

if you need to access information about the event, then yes, pass it in.

And in what case I would want to access info about the event in question...that is,keyup.

Never heard before of such an occasion.

Can you give me an example?

Link to comment
Share on other sites

Look at the methods and properties on the event object. You need to understand what event objects can do, and then you can decide for yourself if you should use it in any given situation. If you don't need any of those methods or properties, then you don't need to deal with the event object. One possible use for any situation is if you have a generic event handler attached to many elements or events and you need to determine which element fired the event or which event was fired. Other than that, look at the event object and decide for yourself.

Link to comment
Share on other sites

Thanks

Link to comment
Share on other sites

There is one last thing I want to address and that is bubbling. Should I also use stopPropagation to prevent bubbling?

I have never addressed so far in my code the issue of event bubbling.

Link to comment
Share on other sites

did you read the docs?

http://api.jquery.com/category/events/event-object/

http://api.jquery.com/event.stopPropagation/

 

if you want to stop it from bubbling, then yes, use it.

Link to comment
Share on other sites

did you read the docs?

http://api.jquery.com/category/events/event-object/

http://api.jquery.com/event.stopPropagation/

 

if you want to stop it from bubbling, then yes, use it.

I read the document about propagation-even before posting and I have to tell you not much is mentioned.

It has only one very simple example.It does not provide enough info...at least to the extent I want.

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...