Monday, 2 July 2018

Joomla Plugin inject Javascript code into all pages

I'm trying to write a Joomla plugin that injects a small javascript code into all pages for distribution to others. but everything I have read says I should do it using the $doc = JFactory::getDocument(); but I can't get it to work.

<?php
defined('_JEXEC') or die('No direct access');

jimport('joomla.plugin.plugin');

class plgSystemCustomhead extends JPlugin{
    function onBeforeCompileHead() {
        /*if (JFactory::getApplication()->isAdmin()){
            return true;
        }*/
        $doc = JFactory::getDocument();
        $doc->addScriptDeclaration('
            (function(a, d, w){
                var h= d.getElementsByTagName(a[0])[0]; var s= d.createElement(a[1]);
                s.setAttribute("type", a[2]); s.setAttribute("src", a[3]); s.setAttribute(a[4], true); h.appendChild(s);
            })(["head", "script", "text/javascript", "//tracker.thisisbeacon.com/tracker/", "async"], document, window);

        '); 
    }
}

All I want to do is have a small plugin people can install that adds our tracking code to all pages on the frontend of a Joomla site.



from Joomla Plugin inject Javascript code into all pages

No comments:

Post a Comment