Refactor JavaScript handling in form renderer #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: JavaScript ReferenceError in Turba Contact Form Tabs
Problem
Users experienced a
ReferenceError: sections_Turba_View_Contact is not definederror when clicking tabs in the Turba contact form. All tabs except the default "personal" tab were unclickable.Root Cause
The issue was a JavaScript timing problem:
form_sections.jsscript was loaded in the HTML header viaHorde_PageOutput::addScriptFile()sections_Turba_View_Contactvariable was initialized in the footer viaHorde_PageOutput::addInlineScript()onclickhandlers were rendered in the body, immediately afterheader()was calledError sequence:
onclick="sections_Turba_View_Contact.toggle(...)"Solution
Approach
Instead of loading the script in the header and initializing the variable in the footer, we:
This guarantees synchronous execution order: Script → Tabs → Variable
Implementation
File:
vendor/horde/form/lib/Horde/Form/Renderer.phpChanges:
$page->addScriptFile('form_sections.js', 'horde')callform_sections.jsfrom filesystem and output directly in bodyCode Flow:
Why This Works
Why Scripts in Header Don't Work
Even though scripts in the header are loaded synchronously (without
asyncordeferattributes), there can be timing issues:_renderSectionTabs()is called afterheader(), the header scripts may not have finished executing yetWhy Inline Scripts Are Necessary
Guaranteed execution order:
Horde_Form_Sectionsexists when neededExecution sequence:
Horde_Form_Sectionsclass is definedonclickhandlerssections_Turba_View_Contactis createdBenefits:
Testing
Before fix:
ReferenceError: sections_Turba_View_Contact is not definedAfter fix:
Technical Details
Before/After Comparison
addScriptFile())addInlineScript())Files Modified
vendor/horde/form/lib/Horde/Form/Renderer.php_renderSectionTabs()methodRelated Issues
Uncaught ReferenceError: sections_Turba_View_Contact is not defined