in reply to Re: Perl - calling javascript funciton
in thread Perl - calling javascript funciton

Perl File snippet: <SCRIPT LANGUAGE="JavaScript" SRC="/js_validation/js.js"></SCRIPT> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +"> </head> <body> <form onsubmit="return formValidator()"> <table> <tr><td> Test Field: <input type="text" value="--" size="2" maxlength="2" id="p +lace"><br></td> </tr> <input type='submit' value='Check Form'><br> Javascript: /* SOURCE FILE: form_validation.js */ function formValidator(){ var place = document.getElementById("place"); if(isNumeric(place, "Please enter a valid zip code")){ return true; } return false; } function isNumeric(elem, helperMsg){ var numericExpression = /^[0-9]+$/; if(elem.value.match(numericExpression)){ return true; }else{ alert(helperMsg); elem.focus(); return false; } } Unfortunately, because I'm a newbie I don't really understand how to u +sefully incorporate the various modules that are out there. I instal +led the JavaScript::DataFormValidator module this morning, but am not + sure how to incorporate it yet. The only modules I am utilizng in m +y program currently are DBI and CGI. I don't really know the details + of what they do. I just know that the tutorials I've been taking to +ld me to use them :) The idea of putting my javascript into a perl string is intriguing. H +owever, it seems that my code could get real messy real quick. Is it + common in Perl to call functions/variables/etc from an external Perl + file (like you can in JS "..SRC="/js_validation/js.js"></SCRIPT>" )? If so, maybe storing my javascript in a variable within an external fi +le would be a good way to do this?

Replies are listed 'Best First'.
Re^3: Perl - calling javascript funciton
by afoken (Chancellor) on Mar 11, 2009 at 15:48 UTC

    Just some small notes:

    • The "language" attribute of the "script" tag is deprecated, use the "type" attribute instead, typically with a value of "text/javascript".
    • XHTML requires lower case tag names, and using them also in HTML does not hurt.
    • The "meta" tag is only useful if you deliver the HTML content via a protocol that does not allow specifying a content type (file://, ftp://). In HTTP context, use the "Content-Type" HTTP header. In E-Mail context, use the "Content-Type" MIME header.
    • You say you use the CGI module. Read the documentation of its header() method.
    • The usage instructions for most Perl modules are embedded into the module, use http://search.cpan.org/ or the perldoc utility to read them.
    • Placing huge loads of javascript into a variable in a module may seem tempting, but consider placing the javascript into an external javascript file, and have the web server deliver it directly. Delivering content from a CGI script is SLOOOOOOOOOOOOOW.
    • Split the javascript code into a huge reusable part in the external file, and a small configurating part inside your delivered HTML.
    • Remember that (in a typical CGI environment) Perl code runs on the server side and Javascript code runs on the client side, after your Perl code has finished. If you want to mix both, you want to learn about AJAX and JSONRPC. That makes some things look better, but it creates lots of new problems. Try learning ordinary CGI first.

    Alexander

    -- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Thank you so much for taking the time to reply. "You say you use the CGI module. Read the documentation of its header( +) method." I just went though it and I understand you can use these predefinted f +unctions to generate the header and footer info. Should I assume tha +t is a much more preferrable way to do it rather than, "print "<html> +<head></head><body>blah, blah...."; ? "The usage instructions for most Perl modules are embedded into the mo +dule, use http://search.cpan.org/ or the perldoc utility to read them +. " The problem is that since I'm just a newbie, I have a hard time unders +tanding what I'm reading. For example, I tried to read about the Jav +aScript::DataFormValidator but didn't really understand how to use it + in my program. "Placing huge loads of javascript into a variable in a module may seem + tempting, but consider placing the javascript into an external javas +cript file, and have the web server deliver it directly. Delivering c +ontent from a CGI script is SLOOOOOOOOOOOOOW. " I actually had an external JavaScript file I was using, but my main is +sue is that I'm unable to utilize it within my Perl file. When I hit + the submit button, my Javascript validation is not working (nothing +happens - should be an alert). What exactly do you mean by "Deliveri +ng content from a CGI script..." Thanks again for the help!