Joomla 3插件addScript没有加载任何东西

时间:2013-06-19 10:14:35

标签: php joomla

我正在写一个简单的joomla插件..它基本上检测一个类并加载一堆css和JavaScript不需要在其他页面上..但它没有加载任何东西......请告诉我我哪里出错了用这个..

继承人PHP:

class plgContentScrollbar extends JPlugin{
    public function __construct(& $subject, $config){
        parent::__construct($subject, $config);
        $this->loadLanguage();
    }
    function onScrollbarPrepare(){
        $app = JFactory::getApplication();
        $bodySite   = JResponse::getBody();
        $scroll =  strpos($bodySite,'scroll-pane');
        if($scroll !== false) {
            $this->onCreateScrollbar();
        }
        return true;
    }
    function onCreateScrollbar(){
        $pluginUrl = JURI::base(true) . '/plugins/content/scrollbar/';
        $document =& JFactory::getDocument();
        $document->addStyleSheet('<link href="'.$urlPlugin.'css/jquery.jscrollpane.css" rel="stylesheet" type="text/css" />');
        $document->addScript('<script type="text/javascript" src="'.$urlPlugin.'js/knockout-2.2.1.js"></script>');
        $document->addScript('<script type="text/javascript" src="'.$urlPlugin.'js/jquery.mousewheel.js"></script>');
        $document->addScript('<script type="text/javascript" src="'.$urlPlugin.'js/jquery.jscrollpane.min.js"></script>');
        $script = 'jQuery(document).ready(function(e) {
            ko.bindingHandlers.jScrollPane = {
                init: function(element, valueAccessor) {
                    var options = valueAccessor() || {};
                    $(element).jScrollPane(options); 
                    $(window).resize(function() {
                        var scroll = $(element).data("jsp");
                        if (scroll) {
                            scroll.reinitialise();
                        };
                    };
                };
            };

            var ViewModel = function() {}
            var pane;
            var remove = function() {
                pane = $("#pane").detach();
            };
            var add = function() {
                $("#content").append(pane);
                $(window).trigger("resize");
            };
            ko.applyBindings(new ViewModel());
        });';
        $style = '.scroll-pane{width: 100%;height: 500px;overflow: auto;}
                .jspVerticalBar {width: 26px;background-color:transparent;}
                .jspVerticalBar .jspArrowUp {background:url(arrow_up.png) no-repeat;width:26px;height:14px;}
                .jspVerticalBar .jspArrowDown {background:url(arrow_down.png) no-repeat;width:26px;height:14px;}
                .jspTrack {background:none;}
                .jspDrag {width:2px;margin-left:13px;background:#000000;}';
        $document->addStyleDeclaration($style);
        $document->addStyleDeclaration($script);
    }

任何帮助非常感谢..谢谢..

1 个答案:

答案 0 :(得分:1)

您将整个脚本标记放入函数中,这是不正确的,请参阅此链接:Adding JavaScriptAdd StyleSheet 您只需提供该文件位置的URL。

试试这样:

$pluginUrl = JURI::base(true) . '/plugins/content/scrollbar/';
$document  = JFactory::getDocument();
$document->addStyleSheet($urlPlugin.'css/jquery.jscrollpane.css');
$document->addScript($urlPlugin.'js/knockout-2.2.1.js');
$document->addScript($urlPlugin.'js/jquery.mousewheel.js');
$document->addScript($urlPlugin.'js/jquery.jscrollpane.min.js');

更新 另请参阅此链接以获取脚本声明。我更喜欢使用它,就像那里显示的第二个例子。链接在这里。

JDocument/addScriptDeclaration