It provides an easy interface to parse and display a document.

First, get a reference to the current document object:

use Joomla\CMS\Factory;
$document = Factory::getDocument();

These two lines are equivalent to the older form:

$document = JFactory::getDocument();

Add Script

To add a JavaScript file, use this code:


The $url is the variable containing the full path to the JavaScript file. For example: JUri::base() . 'templates/custom/js/sample.js'

You can also add $options and $attributes parameters.

Add Script Declaration

If your JavaScript or CSS are generated using PHP, you can add the script or stylesheet directly into the head of your document.

// Add styles
$style = 'body {'
. 'background: #00ff00;'
. 'color: rgb(0,0,255);'
. '}';

Get Document Properties

1. Return the base URI of the document.

echo $document->getBase();

2. Returns the document charset encoding.

echo $document->getCharset();

For example, utf-8

3. Return the meta description of the document.

echo $document->getDescription();

4. Returns the document direction declaration.

echo $document->getDirection();

For example, ltr

5. Returns the document generator

echo $document->getGenerator();

6. Returns the document language.

echo $document->getLanguage();

For example, en-gb

7. Gets a meta tag.

Format: getMetaData(string $name, string $attribute = 'name')

echo $document->getMetaData('generator');

8. Return the document MIME encoding that is sent to the browser.

echo $document->getMimeEncoding();

For example, text/html

9. Return the title of the document.

echo $document->getTitle();

10. Returns the document type

echo $document->getType();

For example, html

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive