Closed Bug 281552 Opened 20 years ago Closed 19 years ago

"Illegal operation on WrappedNative prototype object" when calling reference to form submit

Categories

(Firefox :: General, defect)

x86
Windows Server 2003
defect
Not set
major

Tracking

()

RESOLVED EXPIRED

People

(Reporter: bleroy, Assigned: bugzilla)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0

It is often useful to perform actions before the form is submitted. For it to
work in all circumstances, setting onsubmit is not enough as it's not called
when programmatically submitting the form.
The usual way to do this is to keep a reference to the default submit function
in a variable and set submit to the new function, where the old one is called
through the reference.
It works in IE, but the call through the reference fails with an unexpected
exception in Firefox 1.0.

I'm setting the severity to major because it has no workaround and prevents a
feature of ASP.NET 2.0 from working propertly with Firefox. We may have to
disable the feature for Gecko browsers if this is not fixed or if a workaround
is not found.

Reproducible: Always

Steps to Reproduce:
1. Navigate to this page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<form id="form1" action="submitcrash.htm" method="get">
	<script type="text/javascript">
	<!--
	var oldSubmit = document.forms["form1"].submit;
	document.forms["form1"].submit = newSubmit;
	
	function newSubmit() {
		alert("Doing something before submitting");
		oldSubmit();
	}
	// -->
	</script>
	<div>
		<input type="button" value="Click me" onclick="this.form.submit()"/>
	</div>
</form>
</body>
</html>

2. Press the button
Actual Results:  
The alert is shown, but the form is not submitted. Instead, I get the following
exception:

Error: uncaught exception: [Exception... "Illegal operation on WrappedNative
prototype object"  nsresult: "0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" 
location: "JS frame :: http://localhost/glop/submitcrash.htm :: newSubmit ::
line 12"  data: no]

Expected Results:  
When the button is pressed, an alert should be shown and the form should be
submitted.
You are violating basic OOP principles here: you separate a method from its
object. The following works:

function newSubmit(f) {
		alert("Doing something before submitting");
		oldSubmit.apply(f);
	}

(...)
onclick="this.form.submit(this.form)"

Or even simpler:

function newSubmit() {
  alert("Doing something before submitting");
  oldSubmit.apply(this);
}
(In reply to comment #2)
Erik, thanks for the workaround, this will get us back on track. Your fast
answer is really appreciated.

I see your point on this breaking OOP, but you can't really expect javascript
programmers to spontaneously understand that this is the problem, can you? So
this should still be fixed (even though we're no longer stuck here).

Furthermore, this workaround does not work in IE, so we'll have to do something
like:
if (oldSubmit) {
	if (oldSubmit.apply) {
		oldSubmit.apply(this);
	}
	else {
		oldSubmit();
	}
}

Thanks again.
(In reply to comment #2)
On a side note, is "apply" EcmaScript-compliant?
(In reply to comment #4)
Here's an improved workaround that better preserves OOP, is cross-browser and
does not rely on the apply method:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<form id="form1" action="submitcrash.htm" method="get">
	<script type="text/javascript">
	<!--
	var theForm = document.forms["form1"];
	theForm.oldSubmit = theForm.submit;
	theForm.submit = newSubmit;
	
	function newSubmit() {
		alert("Doing something before submitting");
		if (this.oldSubmit) {
			this.oldSubmit();
		}
	}
	// -->
	</script>
	<div>
		<input type="button" value="Click me" onclick="this.form.submit()"/>
	</div>
</form>
</body>
</html>
This is an automated message, with ID "auto-resolve01".

This bug has had no comments for a long time. Statistically, we have found that
bug reports that have not been confirmed by a second user after three months are
highly unlikely to be the source of a fix to the code.

While your input is very important to us, our resources are limited and so we
are asking for your help in focussing our efforts. If you can still reproduce
this problem in the latest version of the product (see below for how to obtain a
copy) or, for feature requests, if it's not present in the latest version and
you still believe we should implement it, please visit the URL of this bug
(given at the top of this mail) and add a comment to that effect, giving more
reproduction information if you have it.

If it is not a problem any longer, you need take no action. If this bug is not
changed in any way in the next two weeks, it will be automatically resolved.
Thank you for your help in this matter.

The latest beta releases can be obtained from:
Firefox:     http://www.mozilla.org/projects/firefox/
Thunderbird: http://www.mozilla.org/products/thunderbird/releases/1.5beta1.html
Seamonkey:   http://www.mozilla.org/projects/seamonkey/
This bug has been automatically resolved after a period of inactivity (see above
comment). If anyone thinks this is incorrect, they should feel free to reopen it.
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → EXPIRED
Summary: uncaught exception when calling reference to form submit → "Illegal operation on WrappedNative prototype object" when calling reference to form submit
You need to log in before you can comment on or make changes to this bug.