Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
28
rated 0 times [  31] [ 3]  / answers: 1 / hits: 15815  / 11 Years ago, sun, march 24, 2013, 12:00:00

I have this example, when my form is submitted I have to load an image file and wait until the image has loaded to return true (in this case to submit the form)



e.g.



$('#myform').submit(function(){
var image=document.createElement('image')
var src=document.createAttribute('src')
image.value=http://example.com/image.jpg
image.setAttributeNode(src);
document.body.appendChild(image);
})


Here if I return true the form will be submitted (the image won't load) and if false it won't (the image will load but the form will not be submitted). How can I make a code that will return true after the image has loaded.


More From » jquery

 Answers
20

There is an important concept one must note here - When you add an Event Handler function to be executed when a particular event is fired, the value that the Event Handler function returns actually goes nowhere, and is passed to nothing. Event Listeners are created to invoke Event Handler functions at an unknown point in the future, but typical script execution is completely linear and happens in the order of the commands in the script - so it is best to define the functionality of your application in a way that you perform certain actions like this only when certain events take place.



It looks like in your question above, you are defining one event handler to listen for when the form is submitted initially, so I will take that as the initial event which gets everything started. Here is how I would handle the submission process you describe:



//wrap the form element in jQuery and cache it so we don't work to re-select it
var $myform = $('#myform');

//specify that we are listening for -when- the form is submit
$myform.on('submit', function(event){
//prevents the form from actually submitting when submit is first clicked
event.preventDefault();

//Simpler image construction
var image = new Image();
document.body.appendChild(image);

//remember to listen for the image's load event before assigning a source
$(image).on('load', function(){
//this function is invoked in the future,
//when the image load event has been fired

//this bit actually submits the form via GET or POST
$myform.submit();
});

//an image's `src` attribute can be set directly via the`src` property
image.src = http://placekitten.com/320/240;
});


Example of this working on JSFiddle:



http://jsfiddle.net/Admiral/uweLe/



I would recommend reading up on jQuery's .on() method to gain some insight on the currently preferred methods of event binding - that should clear things up a bit.



http://api.jquery.com/on/



Good luck!


[#79384] Saturday, March 23, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janjadonb

Total Points: 4
Total Questions: 114
Total Answers: 118

Location: Mali
Member since Fri, Dec 3, 2021
3 Years ago
janjadonb questions
;