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.
You can not hang Safari and Firefox4+ with the while loops which is what I am guessing you are doing in your sleep function.
Only real way you can hang the browser would be making a synchronous Ajax call instead of the image request. Bad thing about the synch call is it will lock the browser until it comes back. Slow connections could mean a long wait before the form submits.
Other option is to submit the form to your server and than forward it to the other server.