471,897 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,897 software developers and data experts.

Redirecting Form.Submit() to an event handler

Hi All,

I have added an event handler to redirect form.submit() to a newSubmit() method that I have defined (which does some additional processing before submitting the form).

Additionally I have defined the relavant function method in the code for details)

The issue is that when Icall targetForm._submit() method from the newSubmit() function the page I get an 'Object doesn't support this property or method' error

I am using IE 6.0.2900.2189 sp2 runniong on Windows 2003 workstation

The souce code is listed below. To run thie code:

1 .Open you IE browser using the supplied html code below code
2. Click the "Register Submit Event" buttion - to register the newSubmit(0 event handler
3. Click the Submit Form button to attempt to submit the form

<html><head><title>Submit Form Fields</title></head>
<body><h2>Submitting the form elements</h2>

<form name="testForm" action="submitResult.html" method="POST">
<input type="text" name="text_entry" value="" >
<input type="button" name="btn_register_submit" value="Register Submit Event" onClick="registerSubmit()">
<input type="button" name="btn_submit_form" value="Submit Form" onClick="submitForm()">

<script language ="Javascript">
function newsubmit(targetForm) {
alert('New submit function invoked');

// This doe not work - get an 'Object doesn't support this property or method' error


// This function registers the new submit event with the form.submit() action so that
// form.submit() isredirected to the newSubmit() instead
function registerSubmit() {

alert('Register Submit');

// Register the submit event (using the traditional event registration model)
// See: http://www.quirksmode.org/js/events_tradmod.html
testForm.submit = function() {newsubmit(testForm)};

// Note the W3C approach to registering event handlers is noted below but
// 1. I cannot get this to work in IE
// 2. I am not sure how to pass arguments to the newSubmit in this case
// See: http://www.quirksmode.org/js/events_advanced.html
//testForm.addEventListener('submit', newSubmit, false); // Mozilla / Opera
//testForm.addEventListener('submit', newSubmit); // IE syntax

// To get the submit to work correctly a function prototype for the submit() needs to be defined
// The W3C recommended syntax for this is noted below:
// See also: http://diveintogreasemonkey.org/patterns/override-method.html

// capture the onsubmit event on all forms
window.addEventListener('submit', newsubmit, true);

// If a script calls someForm.submit(), the onsubmit event does not fire,
// so we need to redefine the submit method of the HTMLFormElement class.
HTMLFormElement.prototype._submit = HTMLFormElement.prototype.submit;
HTMLFormElement.prototype.submit = newsubmit;

// Issue : this does not work when running in IE. The recommended synatx in IE is as noted below:
// See: // http://delete.me.uk/2004/09/ieproto.html

// Emulate the prototyping behaviour in IE 6.0.2900.2189 sp2
Element = function () {};
Element.prototype._submit = function () {Element.prototype.submit};
Element.prototype.submit = newsubmit;

// Debug - uncomment to view the function prototype contents
//alert('Element.prototype._submit =' + Element.prototype._submit);
//alert('Element.prototype.submit =' + Element.prototype.submit);


function submitForm() {
alert('Submit form called');

Aug 6 '07 #1
1 10589
16,027 Expert Mod 8TB
Moved from the Articles section.
Aug 6 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Halldór Ísak Gylfason | last post: by
2 posts views Thread by Andy Goldstein | last post: by
6 posts views Thread by varkey.mathew | last post: by
4 posts views Thread by josh.dutcher | last post: by
2 posts views Thread by patrick73 | last post: by
4 posts views Thread by sameergn | last post: by
3 posts views Thread by prodizy | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.