Can't load image using javascript within onsubmit form event on safari

I want to load an image with JS within the form event “onsubmit” without stipping the submit action of the form.

What I do is the following:

var img = new Image();
img.onload = function(){
myfunction();
};
img.src = URL_FOR_PHP_SCRIPT_TO_DO_SOME_LOGIC;
window_sleep(750); //Custom method to hang the CPU for 750ms until the request to call the image is called.

This logic is a MUST for me, I mean I don’t want to use settimeout to postpone the form submit, and I have to call the URL_FOR_PHP_SCRIPT_TO_DO_SOME_LOGIC this way since this code is on a JS file from different domain. This logic works fine on all browsers, and the request to call the image is sent -which is what exactly I need-, however on safari it doesn’t call the image request, and start posting the form directly. I tried to add an “onunload” event on the page containing the form when the browsers is safari, but this didn’t help too, and the form still submitting data directly without starting sending the request to load the image.

returning false from onsubmit of the form will stop the form submission. you can take advantage of this.
For example, load the image in the form’s onsubmit() and return false. Then in the image’s load event, submit the form programatically (myform.submit() ) .