waris has asked for the wisdom of the Perl Monks concerning the following question:

I'm creating a perl-generated html form. It contains a number of javascript functions including a script that runs onLoad, another one onSubmit and multiple functions which use onBlur, onFocus etc... The form as generated works ok but none of the javascript functions. If I take the perl generated html and rename it as an html file, it all functions perfectly.

From research, I'm assuming I need to use "Content-type: application/x-javascript\n\n" but haven't been able to find any details. Does it go in the head section of the html immediately in front of all the javascript functions? Does it need to be repeated for each function? And/Or does it need to be embedded in the body wherever a javascript function is called? Or am I totally incorrect about what is needed? My experiments thus far have all failed and I suspect I'm missing something simple.

A sample, here is the onSubmit statement:
onSubmit="return (CompareD(this) && valisort(this));"
and here are the functions in the head of the html
<SCRIPT language="JavaScript"> function CompareD(thd){ //function details } </script> <SCRIPT language="JavaScript"> function valisort(pmp){ //function details } </script>
If someone could show me where/how to use the content type statement back and forth between javascript and html in this example, I would be eternally grateful! (I realize that some of the functions could be totally re-created in Perl but clientside onBlur and onFocus for instance would be difficult - at least for me!)

edited: Tue Dec 9 20:59:15 2003 by jeffa - code tags and formatting

Replies are listed 'Best First'.
Re: embedding Javascript in perl generated html
by janjan (Beadle) on Dec 09, 2003 at 19:59 UTC
    You shouldn't need to use the Content-type line before each Javascript call, or at all in fact -- the <script> tags should do exactly what you need.

    Sometimes I find that when including javascript in perl-generated html, I forget to escape things properly in the javascript functions. Have you tried viewing source on the generated page to verify that you're getting what you think you are?
Re: embedding Javascript in perl generated html
by waswas-fng (Curate) on Dec 09, 2003 at 19:24 UTC
    If you have a unix box and LWP installed "GET" the html from the cgi app and then compare it to the html you generate by hand. You do not need to use Content-type: application/x-javascript but you do need to make sure it is located in the properly formed head section. I tend to do an include on a core JS file with many of the reusable functions and just place the functions that are not global in the template or html producing functions.


    -Waswas
Re: embedding Javascript in perl generated html
by Theo (Priest) on Dec 10, 2003 at 15:39 UTC
    If I take the perl generated html and rename it as an html file, it all functions perfectly.

    Sounds like your perl generated form does not end in ".HTML". So what does it end in and why don't you have perl generate it with the proper ending in the first place?

    Or am I mis-understanding something here? (happens all the time)

    -theo-
    (so many nodes and so little time ... )

Re: embedding Javascript in perl generated html
by Cody Pendant (Prior) on Dec 09, 2003 at 20:17 UTC
    Client-side focus and blur would be impossible with Perl, you know that, right?

    Show us the code.



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print