I have a form that is displayed only when a particular link is clicked, and the options for a number of dropdown menus are populated by javascript.
I need to run this javascript when the form loads, but I can only find examples where it is put in the element and from what I understand, CGI Perl creates the tag in the start_html function.
Is there a way that I can trigger a javascript function only when the form is displayed?
Edit:
The Form has two rows of dropdown menus. the javascript populates the options for the second row based on the selectedIndex values of the first row. I want the second row to be populated based on the defaults in the first row when the page loads.
If the JavaScript function that populates the fields is also included conditionally by the same logic that includes (or not) the form, then you can test whether the function exists and if so call it:
If your function exists all of the time (perhaps it is in an external JS file that is always included on the page) then give your form an id (if it doesn’t already have one):
Then you can test whether the form exists and call your function to do the population:
In either case, the JS code that I’ve shown would need to be either inside an
onloadhandler or in a script block that appears after the form, because JS can only manipulate elements that the browser has already parsed.(If the function itself is defined in a script block that appears after the form, and is included conditionally, then you can just call it directly right after defining it.)