By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

Hooking into page's onsubmit event from code within page?

P: n/a
Hi All,

Where I work we're using a reasonably basic tool to build web pages
and forms, that doesn't allow us to edit any source HTML and doesn't
allow us to access header content or the <BODYtags at all.

We've discovered that one of the 'field types' we can add to a form
will allow us to include javascript functions and also HTML elements
that appear to be accurately interpreted when the page is rendered,
but our most pressing need at the moment is to be able to figure out a
way to perform form field validation of the form controls when the
page is submitted.

As above, I can't edit any of the properties of the submit button
itself, and can't edit properties of the BODY tag or anything that
appears within the HEAD tags. I can only insert Javascript as a text
element into the page itself.

Does anyone know if I can implement some javascript that gets run when
the page is loaded that enforces running of another javascript
function when the page is submitted?

Any help much appreciated!!

pt
Oct 3 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Oct 3, 6:57*am, planetthoughtful <planetthought...@gmail.com>
wrote:
but our most pressing need at the moment is to be able to figure out a
way to perform form field validation of the form controls when the
page is submitted.
Try this:
-------------
function addEvtLis(o, type, handler) {
if(o.attachEvent) {
o.attachEvent("on" + type, handler);
}
else if(o.addEventListener) {
o.addEventListener(type, handler, false);
}
}

function formOnSubmitHandler(evt){
var validationPassed = true;

//your code

//prevent form submission
if( !validationPassed ){
if(evt.preventDefault) evt.preventDefault();
evt.returnValue = false;
}
}

//set handler for form submit
addEvtLis(document.myFormName, "submit", formOnSubmitHandler);
--------------

>I can only insert Javascript as a text element into the page itself
Hope script tags are intact, otherwise no javascript will work.

Does anyone know if I can implement some javascript that gets run when
the page is loaded that enforces running of another javascript
function when the page is submitted?
--------
function pageOnLoadHandler(evt){
//your code
}

//set handler for window on load
addEvtLis(window, "load", pageOnLoadHandler);
-------

- Kiran Makam
Oct 3 '08 #2

P: n/a

On Oct 3, 3:40*pm, Kiran Makam <kiranm...@gmail.comwrote:
On Oct 3, 6:57*am, planetthoughtful <planetthought...@gmail.com>
wrote:
but our most pressing need at the moment is to be able to figure out a
way to perform form field validation of the form controls when the
page is submitted.

Try this:
-------------
function addEvtLis(o, type, handler) {
* * if(o.attachEvent) {
* * * * o.attachEvent("on" + type, handler);
* * }
* * else if(o.addEventListener) {
* * * * o.addEventListener(type, handler, false);
* * }

}

function formOnSubmitHandler(evt){
* * var validationPassed = true;

* * //your code

* * //prevent form submission
* * if( !validationPassed ){
* * * * if(evt.preventDefault) evt.preventDefault();
* * * * evt.returnValue = false;
* * }

}

//set handler for form submit
addEvtLis(document.myFormName, "submit", formOnSubmitHandler);
--------------
I can only insert Javascript as a text element into the page itself

Hope script tags are intact, otherwise no javascript will work.
Does anyone know if I can implement some javascript that gets run when
the page is loaded that enforces running of another javascript
function when the page is submitted?

--------
function pageOnLoadHandler(evt){
* * //your code

}

//set handler for window on load
addEvtLis(window, "load", pageOnLoadHandler);
-------

- Kiran Makam
Hi Kiran,

I should have mentioned that your solution hit the spot exactly!

Thank you very much!

pt
Oct 13 '08 #3

P: n/a
On Oct 3, 1:40*am, Kiran Makam <kiranm...@gmail.comwrote:
On Oct 3, 6:57*am, planetthoughtful <planetthought...@gmail.com>
wrote:
but our most pressing need at the moment is to be able to figure out a
way to perform form field validation of the form controls when the
page is submitted.

Try this:
-------------
function addEvtLis(o, type, handler) {
* * if(o.attachEvent) {
* * * * o.attachEvent("on" + type, handler);
* * }
* * else if(o.addEventListener) {
* * * * o.addEventListener(type, handler, false);
* * }

}
Always use typeof to test host object methods.
>
function formOnSubmitHandler(evt){
* * var validationPassed = true;

* * //your code

* * //prevent form submission
* * if( !validationPassed ){
* * * * if(evt.preventDefault) evt.preventDefault();
* * * * evt.returnValue = false;
Don't augment host objects.

Oct 13 '08 #4

P: n/a
On Oct 13, 10:29*am, David Mark <dmark.cins...@gmail.comwrote:
On Oct 3, 1:40*am, Kiran Makam <kiranm...@gmail.comwrote:
On Oct 3, 6:57*am, planetthoughtful <planetthought...@gmail.com>
wrote:
but our most pressing need at the moment is to be able to figure out a
way to perform form field validation of the form controls when the
page is submitted.
Try this:
-------------
function addEvtLis(o, type, handler) {
* * if(o.attachEvent) {
* * * * o.attachEvent("on" + type, handler);
* * }
* * else if(o.addEventListener) {
* * * * o.addEventListener(type, handler, false);
* * }
}

Always use typeof to test host object methods.
Is there any case where a typeof would have distinct benefit over the
way Kiran has done?
function formOnSubmitHandler(evt){
* * var validationPassed = true;
* * //your code
* * //prevent form submission
* * if( !validationPassed ){
* * * * if(evt.preventDefault) evt.preventDefault();
* * * * evt.returnValue = false;

Don't augment host objects.
Don't `supplement/overuse' host objects? [Sorry, but English isn't my
first language]

/sasuke
Oct 13 '08 #5

P: n/a
On Oct 13, 12:13*pm, sasuke <database...@gmail.comwrote:
On Oct 13, 10:29*am, David Mark <dmark.cins...@gmail.comwrote:
On Oct 3, 1:40*am, Kiran Makam <kiranm...@gmail.comwrote:
On Oct 3, 6:57*am, planetthoughtful <planetthought...@gmail.com>
wrote:
but our most pressing need at the moment is to be able to figure out a
way to perform form field validation of the form controls when the
page is submitted.
Try this:
-------------
function addEvtLis(o, type, handler) {
* * if(o.attachEvent) {
* * * * o.attachEvent("on" + type, handler);
* * }
* * else if(o.addEventListener) {
* * * * o.addEventListener(type, handler, false);
* * }
}
Always use typeof to test host object methods.

Is there any case where a typeof would have distinct benefit over the
way Kiran has done?
function formOnSubmitHandler(evt){
* * var validationPassed = true;
* * //your code
* * //prevent form submission
* * if( !validationPassed ){
* * * * if(evt.preventDefault) evt.preventDefault();
* * * * evt.returnValue = false;
Don't augment host objects.

Don't `supplement/overuse' host objects? [Sorry, but English isn't my
first language]

/sasuke
Don't add arbitrary properties to host objects. Treat them like they
are from another planet (and very sensitive.)

At the very least, an else should be added to the nested if.
Oct 13 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.