0

I have read lot of threads about the timing and the handling of events in javascript, but I haven't found the solution for my problem yet or at least I'm an idiot :(.

Here are the list of the references :

  1. jQuery show() function is not executed in Safari if submit handler returns true
  2. JavaScript commands not executed in order in Safari
  3. Why is setTimeout(fn, 0) sometimes useful?
  4. setTimeout with zero delay used often in web pages, why?

I have the latest version of OsX (Mountain Lion) and latest version of safari and the following code:

function handler(btn, form,content){
var submitHandler = function(){
    form.submit(function(e){
        return true;
    });
};

btn.click(function(e){
    form.children().hide();       //command1
    content.show();               //command2
    setTimeout(submitHandler,0);  //command3
});

}

I have created a JsFiddle example also to represent my problem: http://jsfiddle.net/nxjohny/t4s5W/1/ Unfortunately, I have to use the submit button with the name="submit" property. So there is no option to preventDefault the click event and after submit the form programaticaly. :(

The code will be executed when the user click on the button and the commands do what they have to do: command1 hide the children elements of the form. (Add style="display: hidden;" to the children elements. command2 will show the hidden part of the form.(Add style="display:block" to the corresponding element). After the event handler accept the form.submit and return true.

What i actually want with this code to Hide and Show the proper elements of the html page. The code will apply the style related parts on the page and after return with the submit without to Hide/Draw the proper Elements.

This code works on Chrome,Firefox in Linux/Windows/OsX And Safari in Windows, except Osx- Safari 6.X.X.

This code works well with Safari 5.X.X and OsX.

What should i add to the code to really hide and display the elements Before the submit?

Every comment would be welcome and grately appreciated!

Best Regards!

Community
  • 1
  • 1
nxjohny
  • 5
  • 2
  • 2
  • Can you change the `name` in JS? – SLaks Jun 17 '13 at 17:38
  • if you have input elements in your form that can be focused, your form can be submitted by bypassing the click event when pressing enter. I suggest changing the name of the submit button and doing it correctly(you can do this with javascript if you have to). – Kevin B Jun 17 '13 at 17:40
  • I have to add the name of the button to the post. It has a special value. So there is no option to change that :( – nxjohny Jun 17 '13 at 17:41

1 Answers1

0

There is a way to submit the form using the magic of call()

function submitMyForm() {

    //reference to the form you want to submit
    var oldForm = document.getElementById("myForm");

    //How you would normally submit the form
    //oldForm.submit(); //Uncaught TypeError: Property 'submit' of object #<HTMLFormElement> is not a function    

    //How you can submit the form with the "submit" naming conflict
    var newForm  = document.createElement('form');  //create a new form and leach off its submit
    newForm.submit.call(oldForm);  //will submit the form without the error above
}

Passing the submit Value to the server

function submitMyForm(oldForm, button) {

    var input = document.createElement("input");
    input.type = "hidden";
    input.name = "submit";
    input.value = button.value || "Submit";
    oldForm.removeChild(button);
    oldForm.appendChild(input);

    var newForm = document.createElement('form');
    newForm.submit.call(oldForm);
}


var frm = document.getElementById("myForm");
var btn = frm.submit;
submitMyForm(frm, btn);

JSFiddle

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • woot :) I have to try this! – nxjohny Jun 17 '13 at 18:14
  • If i do the submit this way the submit parameters won't be the same :(. I tried it with Tamper Data and i couldn't find with this solution the submit=" " post parameter when i submit the new form. – nxjohny Jun 17 '13 at 18:32
  • Well the button would not be submitted to the server since it was not clicked. A way around it is to add a hidden field with the name/value you expect on the server. – epascarello Jun 17 '13 at 18:45
  • If i add to the submit="someValue" parameter to the newForm that will be the same situation as the previous, isnt it? – nxjohny Jun 17 '13 at 18:48
  • Just added some new code which will add a hidden element. If you need to pass in different buttons, than you can just pass in a reference to the button you need to say submitted the form. – epascarello Jun 17 '13 at 18:56
  • Thank you!Is there any way to force the submit handler before return to wait the callback of the show function? :P – nxjohny Jun 17 '13 at 20:59
  • Add a timeout to wait a second or two for the page's DOM to update. – epascarello Jun 17 '13 at 21:07
  • This Solution worked perfectly! Is there any chance to redefine the form.submit.call(arg) to work on Internet explorer 7? I had a problem with only ie7. :( it throws an error and complaining the method call on form.submit – nxjohny Jul 29 '13 at 18:23