When a Asp.Net aspx page on my site loads, a Javascript script runs a check on some condition, and if that condition is met, it should set an input element's value programmatically, and then submit the form, also programmatically, using form.submit(). With this, I am not using a click or submit event. The form should be processed backend and then the page reloaded with the correct data (and it does this correctly).
For some reason, this seems to work fine in FireFox, but it behaves differently in Chrome.
What is does in Firefox is that after the form.submit() the page reloads and my Javascript gets kicked off from the start (running the check again), as expected.
However, on Chrome, it seems that after the form.submit() call, the Javascript just continuous with the remaining Javascript code.
I tried the form.requestSubmit(), but that didn't change it.
My code:
// check if someCondition is true, if so, a form field needs to be set prgrammatically and then the form submitted. If condition is false, just continue
if(someCondition){
VerifyBeforeCheck();
}
DoTheNextThingIfConditionIsFalse(); //In Firefox, if VerifyBeforeCheck() is fired/ reached, this code never gets executed. In Chrome it will, but I don't want that.
// simple wrapper function, because SubmitMyForm gets called from different events and methods.
function VerifyBeforeCheck() {
let newValue = sessionStorage.getItem("ItemFromSessionStorage");
let fld = document.getElementById("formInputField"); // grabs an <input> field
field.value = newValue; // used in backend
SubmitMyForm();
}
function SubmitMyform(){
let form = document.getElementById('myForm');
form.submit();
}
Is this indeed unexpected behavior or am I missing something? I want the situation in Chrome to be as it is currently when I run it in Firefox: After the submit method, the form is submitted and then a page reload. If I need to use a preventDefault, how can I do this, without the event?