Mackelito
Mackelito

Reputation: 4421

Add js in Joomla 2.5 plugin

I just trying to add a js file but I cant get it to work... I have googled but not found any help yet.. any help is much appreacheated

This is my code:

<?php

defined('_JEXEC') or die('Restricted access');
jimport('joomla.plugin.plugin');

class plgSystemInfinityScroll extends JPlugin {
protected $_execute;

function __construct(&$subject, $config) {
    $app = JFactory::getApplication();

    if($app->isAdmin())
    {
        return;
    }

    parent::__construct($subject, $config);
    $this->loadLanguage('', JPATH_ADMINISTRATOR);
    $this->_execute = true;
}

public function onBeforeCompileHead() {

    $document =& JFactory::getDocument();
    $document->addScript('/plugins/system/sjdinfinitescroll/jquery.infinitescroll.js');

}

public function onAfterRender() {

}

}

Upvotes: 0

Views: 1381

Answers (1)

Stilero
Stilero

Reputation: 461

First of all, Id use the plugin event:

function onBeforeRender(){

}

Secondly your path is pointing wrong.

Change it to:

//J1.6+
$script= JURI::root(true).DS.'plugins'.DS.'system'.DS.'sjdinfinitescroll'.DS.'jquery.infinitescroll.js';
$document =& JFactory::getDocument();
$document->addScript($script);

If you have MooTools loaded on the same page, you have to use jQuery.noConflict(); to avoid conflicts.

All together:

function onBeforeRender(){
      $script= JURI::root(true).DS.'plugins'.
           DS.'system'.DS.'sjdinfinitescroll'.DS.'jquery.infinitescroll.js';
      $document =& JFactory::getDocument();
      $document->addScript($script);
      $document->addScriptDeclaration('jQuery.noConflict();');
      $document->addScriptDeclaration('jQuery(function($){ $('#myid'.append('<h2>my header</h2>');} );');
}

Upvotes: 1

Related Questions