SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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() ) .


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •