Joomla Document Class

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();

Add Script or Stylesheet

To add a JavaScript file:


To add a stylesheet:


The $url is the variable containing the full path to the JavaScript file. You can also add $options and $attributes parameters. The $options control overall how the <script> and <link> elements are output while the $attributes get set as HTML attributes within those tags.

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. $document->getBase(); 

Return the base URI of the document.

2. $document->getCharset();

Returns the document charset encoding. For example, utf-8.

3. $document->getDescription();

Return the meta description of the document.

4. $document->getDirection();

Returns the document direction declaration. For example, ltr.

5. $document->getGenerator();

Returns the document generator

6. $document->getLanguage();

Returns the document language. For example, en-gb.

7. $document->getMetaData($name, $attribute = 'name');

Gets a meta tag.

8. $document->getMimeEncoding();

Return the document MIME encoding that is sent to the browser. For example, text/html.

9. $document->getTitle();

Return the title of the document.

10. $document->getType();

Returns the document type. For example, html.