3

I am transforming a HTML template into a Joomla template. I have the following block in HTML:

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

What is the best way of putting this conditional statement in a JDocument object?

1
  • What about leave the code as is? Should we use JDocument object anyway?
    – Farahmand
    Oct 3, 2014 at 15:49

2 Answers 2

3

I believe the following should work just fine:

<?php $doc = JFactory::getDocument(); ?>
<!--[if lt IE 9]>
<?php
    $doc->addScript('https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js');
    $doc->addScript('https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js');
?>
<![endif]-->

Note that I've defined $doc outside the statement, just in case you require it elsewhere.

Update for example:

<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

<jdoc:include type="head" />

Hope this helps

4
  • addScript will at the files to an array and later render them along with other files. The conditional comments will be left outside. So this does not work. But thanks! Oct 3, 2014 at 13:49
  • Ahh fair enough. Just out of curiosity, why do you want to use JDocument to import scripts from a CDN? Surely the code you posted in your question would suffice
    – Lodder
    Oct 3, 2014 at 13:53
  • It helps me avoid conflicts / order of loading if I can load them all with JDocument. Anyway the code itself could also be local not just from a CDN. Oct 3, 2014 at 13:55
  • 1
    Personally, I always load these script via CDN using normal script tags then below that, use <jdoc:include type="head" /> so the other script that are imports using JDocument are always imported after.I've provided an example in my answer to show you. This is also how it's done in the Protostar template however they decided to import html5shiv after rather than before
    – Lodder
    Oct 3, 2014 at 14:05
4

It looks like JDocument (actually JDocumentHTML) has a hidden method: addCustomTag.

$customTag = <<<CUSTOM
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
CUSTOM;
$doc->addCustomTag($customTag);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.