PHP Joomla! 6 Joomla! 5 Joomla! 3 (Legacy) Template Basic

Inject Inline JavaScript into the Document with WebAssetManager

Inserts inline JavaScript via addInlineScript() with automatic CSP nonce support and ordered execution.

Updated on 24 November 2026 42 copies 166 views
templates/seu_template/index.php
<?php

declare(strict_types=1);

use Joomla\CMS\Factory;

$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$wa->addInlineScript("
    document.addEventListener('DOMContentLoaded', () => {
        // Seu código JavaScript
    });
");
Supports automatic CSP nonces and clean ordering inside WebAssetManager.
templates/seu_template/index.php
<?php
$document = JFactory::getDocument();
$document->addScriptDeclaration('
    // código javascript
');
Legacy Joomla 3 snippet via $document->addScriptDeclaration().

Technical explanation and best practices

Modern replacement for addScriptDeclaration(). When passing server-side configuration into JavaScript, pair this with $doc->addScriptOptions() rather than concatenating raw PHP variables inside JS strings.

Direct Engineering

Have a Joomla project or technical challenge in mind?

From architectural diagnosis to production deployment, directly with the engineer.