onSubmit and Internet Explorer = trouble

  • Thread starter Mark Livingstone
  • Start date
M

Mark Livingstone

I have a form that uses the following: onSubmit="some_var =
'validated';"

FireFox is OK with that. Internet Explorer isn't. any ideas why?

Thanks.
 
V

VK

I have a form that uses the following: onSubmit="some_var =
'validated';"

FireFox is OK with that. Internet Explorer isn't. any ideas why?

This code assigns to global variable some_var string value "validated"
and then submits the form so causing new page load with the Javascript
context being reset: so it doesn't matter what value to what do you
assign before that. I don't know what browser and how could be "OK
with that" - you have to explain then what does it mean OK and most
helpful: what are you trying to achieve overall.
 
M

Mark Livingstone

This code assigns to global variable some_var string value "validated"
and then submits the form so causing new page load with the Javascript
context being reset: so it doesn't matter what value to what do you
assign before that. I don't know what browser and how could be "OK
with that" - you have to explain then what does it mean OK and most
helpful: what are you trying to achieve overall.

I am using jQuery Forms script to bind to the submit action. so, when
the onSubmit fires, the script picks it up, runs a certain function
and prevents the form from being submitted using the regular POST
method -- it does that using jQuery's AJAX post method. page doesn't
refresh and JS context is not reset. So, FF is OK with that syntax and
I can recognize the value of some_var but IE doesn't.
 
M

Mark Livingstone

Forgot to mention... if I create a function that assigns a value to
some_var and then do onSubmit="function();", everything works fine.
It's just that IE doesn't want to recognize onSubmit="some_var =
'value';" or onSubmit="somevar = 1;" syntax.
 
V

VK

Forgot to mention... if I create a function that assigns a value to
some_var and then do onSubmit="function();", everything works fine.
It's just that IE doesn't want to recognize onSubmit="some_var =
'value';" or onSubmit="somevar = 1;" syntax.

If somewhere after page load you do like
document.forms[0].onsubmit = validate;
then it is not important what do you have in the form intrinsic event
listener because it gets overridden. If you have
<form ... onsubmit="some_var='value';">
then this listener is being overriden so never called. This is
consistent across browsers including Firefox, so whatever it is "OK
with that" - it is not what you are describing in this thread. If you
need to assign a value and call form validator and prevent form
submission if not validated then you could use something like:
<form ... onsubmit="return validate(some_var='value')">
 
E

Evertjan.

VK wrote on 01 jun 2008 in comp.lang.javascript:
This code assigns to global variable some_var string value "validated"
and then submits the form so causing new page load with the Javascript
context being reset: so it doesn't matter what value to what do you
assign before that.

This is not always true, VK, try this:

<form onSubmit="some_var = 'validated';" target='_blank' ...

And could be usefully used like this:

==========================================
<form onSubmit="return submitOnlyOnce()" target='_blank' ...

<script type='text/javascript'>
var submitted = false;
function submitOnlyOnce() {
if (submitted) return false;
return submitted = true;
};
</script>
==========================================
 
V

VK

VK wrote on 01 jun 2008 in comp.lang.javascript:



This is not always true, VK, try this:

<form onSubmit="some_var = 'validated';" target='_blank' ...

And could be usefully used like this:

==========================================
<form onSubmit="return submitOnlyOnce()" target='_blank' ...

<script type='text/javascript'>
var submitted = false;
function submitOnlyOnce() {
if (submitted) return false;
return submitted = true;};

</script>
==========================================

You missed the point: of course the intrinsic event handler can be
most useful. I explained that one cannot have two unrelated blocks in
both DOM interface handler and in intrinsic handler: one will be
ignored, namely the intrinsic one.

Doesn't work:

....
document.forms[0].onsubmit = validate;
....
<form ... onsubmit="some_var='value';/* will be ignored */">


Workaround 1:

document.forms[0].onsubmit = function() {
some_var='value';
return validate(this);
}

Workaround 2:

<form ... onsubmit="some_var='value'; return validate(this);">

If some_var is indeed some additional "submission allowed" flag then
it could be even:

<form ... onsubmit="return some_var : validate(this);">

There is a number of other options but nothing of what OP was trying
to do: it is simply not supported.
 
V

VK

If some_var is indeed some additional "submission allowed" flag then
it could be even:

<form ... onsubmit="return some_var : validate(this);">

A rush of typing, sorry, of course:

<form ... onsubmit="return some_var ? true : validate(this);">
or
<form ... onsubmit="return some_var ? false : validate(this);">

depending on some_var being allowing or blocking flag.
 
T

Thomas 'PointedEars' Lahn

VK said:
A rush of typing, sorry, of course:

<form ... onsubmit="return some_var ? true : validate(this);">
or
<form ... onsubmit="return some_var ? false : validate(this);">

depending on some_var being allowing or blocking flag.

A more reasonable and more efficient approach would be

<form ... onsubmit="return !!some_var || validate(this);">
or
<form ... onsubmit="return !some_var && validate(this);">

or using some_var in a gauntlet within validate().


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top