简体   繁体   English

的Joomla! 在文章的文档标题中添加脚本

[英]Joomla! add script in document head from article

We have a couple of pages that require special care, jquery-ui will be called from external scripts which are going to "somehow" be added to the head section of an article. 我们有几个页面需要特别注意,将从外部脚本调用jquery-ui,这些脚本将“以某种方式”添加到文章的开头。

I've attempted with jumi, however it isn't the best choice(including a js in stead of php would render it in html body), the only way I could add a javascript file was by including a php file which would echo a , but as one would imagine, this isn't elegant nor efficient in terms of performance. 我尝试使用jumi,但这不是最佳选择(包括js而不是php会将其呈现在html主体中),我可以添加javascript文件的唯一方法是通过包含将回显a的php文件,但可以想象,这在性能上既不优雅也不高效。

Another attempt was, in stead of echoing a script, I've tried using: 另一种尝试是代替使用脚本,而是尝试使用:

<?php
  $document = &JFactory::getDocument();
  $document->addScript( "path/to/jsfile.js" );
?>

but it didn't work as I've expected, it seems that joomla creates the head section before this php script has the chance of being executed. 但是它没有按我预期的那样工作,似乎joomla在此php脚本有机会执行之前创建了head节。

I've also gave easy header a go, however, it seems that it will include the files in all articles, which I do not wish since it will have a pretty big impact in terms of bandwidth and possible javascript issues down the road. 我还提供了简单的标头,但是,似乎所有文章中都包含文件,我不希望这样做,因为它将对带宽和将来可能出现的JavaScript问题产生很大的影响。

I'm farily new to joomla so anything that would provide some flexibility is good as an answer. 我是joomla的新手,所以提供一些灵活性的任何方法都可以作为答案。

If something isn't unclear, please ask, I will try to answer the best I can. 如果有不清楚的地方,请询问,我将尽力回答。

Please note that I'm using joomla 1.7 and php5. 请注意,我正在使用joomla 1.7和php5。

Jumi uses the onAfterRender event (looking at the 2.0.6 plugin) - by this time I think the <head> tag has already been written out, in-fact the whole document is already rendered. Jumi使用onAfterRender事件(查看2.0.6插件)-到这个时候,我认为<head>标记已经被写出,实际上整个文档已经被渲染。

You could try getting the document body and then searching for the closing tag </head> and inserting the script link before it. 您可以尝试获取文档正文,然后搜索结束标记</ head>并在其之前插入脚本链接。 Something like this: 像这样:

$myJS    = "<script type='text/javascript' src='http://mysever.com/my.js'>"
$content = JResponse::getBody(); // gets the html in it's ready to send to browser form
$hdPos   = strpos($content, '</head>');
$hdPos  += 7;  //move position to include the <head> tag
$bodyLen = strlen($content);
$content = substr($content, 0, $hdPos) . $myJS . substr($content, $hdPos, $bodyLen);
JResponse::setBody($content);

NB: This is untested and I don't use Jumi these days but it should be close. 注意: 这是未经测试的,这些天我不使用Jumi,但是应该很近。

You didn't have to go through all this! 您不必经历所有这一切! Go to: extensions -> template manager -> templates tab (its on "styles" by default) -> go to your template and click on "edit HTML". 转到:扩展->模板管理器->模板标签(默认情况下为“样式”)->转到模板,然后单击“编辑HTML”。 You'll be able to add your code directly in the header, and it will be loaded in all the pages. 您将能够直接在标头中添加代码,并将其加载到所有页面中。

A bit more elegant way is to define a function that does what you want in the header - and call it from the body of the specific article you want. 更为优雅的方法是定义一个函数,该函数可以执行标题中所需的操作,然后从所需特定文章的正文中调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM