开发者

Joomla - Insert .js and .css files into a single Joomla article?

Is it possible for a single page on a Joomla website to include it's own custom .js and .c开发者_如何学Css files?

I basically would like to add two custom javascript and css files for a particular page. I don't want these files included into any other Joomla pages.

Any suggestions would be appreciated.

Thank you


Try using a custom code extension such as JUMI. It is designed exactly for this purpose.

From the description: With Jumi you can include php, html, javascript scripts into the modules position, articles, category or section descriptions, or into your own custom made component pages.


The solution from Soygul wont result in proper HTML since these statements / includes belong to the HTML header.

Use : http://extensions.joomla.org/extensions/edition/custom-code-in-modules/11936 This plugin allows inserting material into the head section of your Joomla web site.

You can then use the menu assignment functionality to just add that to certain pages.


Its quite easy to write a simple module like that for yourself - but since this seems already available go with that one. If it doesn't fit your needs :

You just need an "empty / hello world" module with these two statements : ( http://docs.joomla.org/Creating_a_Hello_World_Module_for_Joomla_1.5 ) ( http://docs.joomla.org/Adding_JavaScript_and_CSS_to_the_page )

// Add a reference to a Javascript file
// The default path is 'media/system/js/'
JHTML::script($filename, $path, $mootools);

// Add a reference to a CSS file
// The default path is 'media/system/css/'
JHTML::stylesheet($filename, $path);


I'm not a big fan of adding new extensions to Joomla unless absolutely necessary. If you do, make sure it's not on Joomla's list of vulnerable extensions, first. Each third-party extension/plugin you add is just one more potential back door for hackers.

To add your own custom CSS for a page, you can either edit your template's master CSS file, or just create your own and link it to the project. Here's how you'd do that:

First, figure out how your CSS files are being called. The actual file names will surely differ from my example, based upon the template you're using, but let's look at the Joomla SYSTEM template, which is located in templates/system. The index.php file controls everything, so open it up and you'll find this line:

<?php
include dirname(__FILE__).DIRECTORY_SEPARATOR.'component.php';
?>

Open component.php and you'll see some code that looks like this:

<head>
    <jdoc:include type="head" />
    <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/general.css" type="text/css" />

You can see the call to include a CSS file in the 3rd line. All you need to do is add another line calling a CSS file you create. Create a new file called /templates/system/css/custom.css (or whatever you like) and rewrite the code segment in component.php to look like this:

<head>
    <jdoc:include type="head" />
    <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/general.css" type="text/css" />
    <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/custom.css" type="text/css" />

Now you can just code out your own CSS in the new custom.css file you created. You can do this with any template system from RocketTheme or YooTheme just as easily. In fact, if you use one of their templates, they probably already have a custom.css file that you can simply add your own code to. Just be aware if you do it that way and then later update the template, you'll lose your code additions. That's why I prefer writing my own file. You can probably do something very similar to include custom JS code, but I tend to avoid JS like the plague, so someone else will have to address how to link out to a custom JS file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜