Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting if value is a function
    primarykey
    data
    text
    <p>I need to test whether the value of a form's <code>onsubmit</code> is a function. The format is typically <code>onsubmit="return valid();"</code>. Is there a way to tell if this is a function, and if it's callable? Using typeof just returns that it's a string, which doesn't help me much.</p> <p><strong>EDIT</strong>: Of course, I understand that "return valid();" is a string. I've <code>replace</code>d it down to "valid();", and even "valid()". I want to know if either of those is a function.</p> <p><strong>EDIT</strong>: Here's some code, which may help explain my problem:</p> <pre><code>$("a.button").parents("form").submit(function() { var submit_function = $("a.button").parents("form").attr("onsubmit"); if ( submit_function &amp;&amp; typeof( submit_function.replace(/return /,"") ) == 'function' ) { return eval(submit_function.replace(/return /,"")); } else { alert("onSubmit is not a function.\n\nIs the script included?"); return false; } } ); </code></pre> <p><strong>EDIT 2</strong>: Here's the new code. It seems that I still have to use an eval, because calling form.submit() doesn't fire existing onsubmits.</p> <pre><code>var formObj = $("a.button").parents("form"); formObj.submit(function() { if ( formObj[0].onsubmit &amp;&amp; typeof( formObj.onsubmit ) == 'function' ) { return eval(formObj.attr("onsubmit").replace(/return /,"")); } else { alert("onSubmit is not a function.\n\nIs the script included?"); return false; } } ); </code></pre> <p>Suggestions on possibly how to do this better?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload