// Wrap JavaScript code with script tag using GeneralUtility in TYPO3 CMS $code = 'console.log("Hello World!");'; $wrappedCode = \TYPO3\CMS\Core\Utility\GeneralUtility::wrapJS($code); // Output the wrapped JavaScript code echo $wrappedCode;
// Wrap jQuery JavaScript code with script tag using GeneralUtility in TYPO3 CMS $code = '$(document).ready(function() { $("#myButton").click(function() { alert("Button clicked!"); }); });'; $wrappedCode = \TYPO3\CMS\Core\Utility\GeneralUtility::wrapJS($code); // Add the wrapped jQuery code to the page header $header = $GLOBALS['TSFE']->getPageRenderer()->getHeaderData(); $header .= $wrappedCode; $GLOBALS['TSFE']->getPageRenderer()->setHeaderData($header);In both examples, the GeneralUtility wrapJS function is used to wrap JavaScript code with a script tag and execute it. The package library for this function is TYPO3 CMS core utility.