0

I am creating a new Joomla template and have run into a problem with linking to external .js files.

    <head>
<jdoc:include type="head" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/system.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/general.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/main.css">
<link rel="stylesheet" type="text/css" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/FLpresentation.css">
<script src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/js/jquery-2.1.4.js"></script>
<script src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/js/FLpresentation.js"></script>
<script src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/js/FLajax.js"></script>

The Above code is included in the head tag and the CSS is working fine when I install my template into joomla. Everything looks how it should but all of my Jquery and Javascript code doesn't function. The only code that does is within tags on the index.php file.

Can anyone tell me what I am doing wrong? I have defined the files and folders in the templateDetails.xml and everything other than js is working fine despite me using the exact same method to link the CSS. Help me, I'm confused.

Ricky Payne
  • 71
  • 1
  • 12

2 Answers2

1

100% JS conflicts. Just check the console! You include the Jquery libary, but Joomla already comes with Jquery. Check the console for errors, check if you have Jquery in output HTML more than 1 time and migrate your code.

Also wrap your code like this:

(function($) {

    $(document).ready(function(){
        // START HERE FOR ON_DOCUMENT_READY

        alert(1);
    });


})(jQuery);

This reserve the $ for your Jquery code.

0

Use the JHtml::script method like in the example below:

$url = Juri::base();// pick the url for my joomla website
JHtml::script($url . 'components/com_revista/prefixfree.min.js');//here i'm using one file in my server, but you could put the url for your script like 
"JHtml::script('http://urlforscriptsource/script.js');"

The code below add JQuery framework on your joomla extension:

JHtml::_('jquery.framework');
JHtml::_('jquery.ui');
JHtml::_('jquery.ui', array('sortable'))

;

Ps: Is better use JQuery() instead of $() in yours JQuery codes, this will avoid conflicts!

vasconssa
  • 61
  • 2
  • 3
  • Wrap the code like above will also avoid conflicts AND you are able to use the $-sign in your code. –  Jul 11 '15 at 11:15