代码之家  ›  专栏  ›  技术社区  ›  Sastrija Victor

在joomla模块中放置jquery插件

  •  1
  • Sastrija Victor  · 技术社区  · 15 年前

    我试图在joomla中创建一个模块,它使用一个jquery插件。我必须在单击模块中的元素时执行Ajax操作。目前我正在指定PHP文件的整个路径。但我知道这是个错误的方法。

    jquery插件中的代码如下。(请注意jquery插件文件中指定路径的第二行)

           $.get(
                "/subdirectory/MyBlog/modules/mod_calendar/getCalendar.php", {
                    month: $("#selectedMonth").val(),
                    year: $("#selectedYear").val(),
                    flag: '-1'
                }, function(data){
                    $("#monthlyCalendar").html(data);
                    $("#monthlyCalendar").show();
                }
            ); 
    

    在jquery插件文件中指定路径的正确方法是什么? 另外,我需要知道把jquery插件文件放在模块中的什么位置。

    3 回复  |  直到 15 年前
        1
  •  1
  •   Sastrija Victor    15 年前
        2
  •  0
  •   Will Mavis    15 年前

    我发现最好的方法是使用juri::root方法创建一个javascript变量,然后我可以使用它。在您的PHP代码中,您可以这样做:

    ?>
    
    <script type="text/javascript">
            var joomlaRoot = '<?php echo JURI::root(); ?>';
    </script>
    
    <?php
    

    然后,您可以在执行Ajax调用时使用该变量。

    至于jquery插件文件放在模块中的位置,您可以将它放在模块目录下的任何位置,然后再次使用juri::root创建指向它的路径并调用 JDocument::addScript method .

    另一方面,您可以考虑使用mootools。它捆绑在乔姆拉!已经。它能够执行Ajax调用。此外,通过使用它,可以避免发生jquery冲突的可能性。

        3
  •  0
  •   Sastrija Victor    14 年前

    Atlast我找到了一个很好的解决方案,可以使用joomla中的jquery来使用Ajax。

    首先,创建一个视图和模型,通过Ajax调用获取所需的HTML。

    然后使用类似下面的jquery代码,只获取所需视图的输出。

    //Code to get the base URL of joomla installation
    szURL = document.URL;
    componentList = szURL.split('/');
    szDocument = componentList[componentList.length-1];
    szURL = szURL.replace(szDocument, "");
    
    //URL to the required component
    url = szURL + "?option=COMPONENT_NAME&view=VIEW_NAME&tmpl=component&uid=" + getRandomValue();
    jQuery.get(url, function(data) {
        jQuery("#mydiv").html(data);
    });
    
    
    //Function to get a random number
    //It is used for Ajax calls from IE; Else IE will use the cache values
    function getRandomValue(){
        return Math.floor(1000000 * (Math.random() % 1))
    }
    

    注意用于Ajax调用的URL。它使用“ tmpl=component “只获取所选组件的HTML,而不使用joomla htmls。