views:

226

answers:

2

I'm trying to create a module in joomla, which uses one jquery plugin. I've to do an ajax operation while clicking on an element in the module. Currently I'm specifying the whole path to the php file. But I know Its a wrong method.

The code in jquery plugin is like this.(please note the second line where the path is specified in the jquery plugin file)

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

What is the correct method to specify the path in a jquery plugin file. Also I need to know where to put my jquery plugin file in the module.

A: 

The best way I found to do it is to use the JURI::root method to create a javascript variable that I can then use. In your php code, you would do something like this:

?>

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

<?php

You can then use that variable when you are doing your AJAX call.

As for where to put the jquery plugin file in your module, you can put it whereever you want under your module's directory and then use JURI::root again to create a path to it and call the JDocument::addScript method.

On a side note, you may consider using MooTools. It comes bundled in Joomla! already. It has the ability to do AJAX calls. Also, by using it, you avoid the possibility of having jQuery conflicts.

Will Mavis
+1  A: 

I found an answer in the following blog.

http://blog.subooa.com/development/joomla-coding/ajax-in-joomla-with-jquery/

Joe