Placing Java Script in the Joomla Head Tags

17 years 6 months ago #22992 by mg13
Does anyone know how to put java script in between head tags of a joomla homepage? I thought it would be easy but I can't seem to find the correct file to insert the code so that the java script is active when someone goes tomy homepage.

Any advice would help.

Please Log in to join the conversation.

17 years 5 months ago #24937 by pointri
This should do it...

[code:1]
global $mainframe, $mosConfig_live_site;
$mainframe->addCustomHeadTag('<acript src="' . $mosConfig_live_site . '"/my/directory/whatever.js" language="Javascript" type="text/javascript"></script>';[/code:1]

Joomlapolitan zealot and a somewhat stealthy, rather passive CB3PD developer.
www.constructionlounge.com Construction Lounge: Online resource and networking portal for the construction and remodeling industries.

Please Log in to join the conversation.

17 years 5 months ago #24938 by pointri
I should add that you don't need to hack up the core CB files to splice in your custom JavaScript. You could wrap the code above into a CB plugin so that it executes on certain user actions (view profile, register, update etc.)

Joomlapolitan zealot and a somewhat stealthy, rather passive CB3PD developer.
www.constructionlounge.com Construction Lounge: Online resource and networking portal for the construction and remodeling industries.

Please Log in to join the conversation.

17 years 5 months ago #24967 by Wolverine
Replied by Wolverine on topic Re:Placing Java Script in the Joomla Head Tags
I found that I was unable to add additional headers inside a CB plugin. I am not sure, but I think the joomla header has already been output by the time the cb plugin is triggered. I could be wrong, but I do know I was unable to add additional headers from my plugins.

Please Log in to join the conversation.

17 years 5 months ago #25010 by pointri
You may be right: I hadn't checked this for execution order, but here's a possible solution....

There is also this helper function inside comprofiler.html.php. Maybe it's there just to handle the core output for XHTML compliance (set in the component's configuration), but it might also affect plugins. I don't remember it being mentioned in the API, anyway...

[code:1]
function addCbHeadTag($ui,$text) {
global $mainframe, $ueConfig;
if ($ui==1 && method_exists($mainframe,"addCustomHeadTag"«») && isset($ueConfig) && $ueConfig) {
$mainframe->addCustomHeadTag($text);
} else {
echo $text;
}
}[/code:1]

Joomlapolitan zealot and a somewhat stealthy, rather passive CB3PD developer.
www.constructionlounge.com Construction Lounge: Online resource and networking portal for the construction and remodeling industries.

Please Log in to join the conversation.

17 years 5 months ago #25013 by mikko
Why would someone want to load JS at head? You can load it pretty much everywhere. However, you cannot apply JS to HTML elements that are loaded after the JS. So loading JS at head seems in most cases not good solution.

mikko

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.801 seconds

Facebook Twitter LinkedIn