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

  • Thread starter planetthoughtful
  • Start date
P

planetthoughtful

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 <BODY> tags 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
 
K

Kiran Makam

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);
 
P

planetthoughtful

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);
--------------


Hope script tags are intact, otherwise no javascript will work.


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

}

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

Hi Kiran,

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

Thank you very much!

pt
 
D

David Mark

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.
 
S

sasuke

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?
Don't augment host objects.

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

/sasuke
 
D

David Mark

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?
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.
 

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,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top