Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Where to edit the head
apbarratt
New
I need to insert a little javascript I've written in between the head tags of every page on the forum. Where can I do that?
Tagged:
0
Best Answer
-
Tim Vanilla Staff
If you're running the default Vanilla theme you can access the header HTML here:
This is the worst way to approach this task I'm afraid. When Vanilla is updated, your code vanishes or causes conflicts.
/applications/dashboard/views/default.master.php
Throw your code in right before the closing</head>
tag.
A better way is to make a small plugin and hook into the Base_Render_Before event, then call AddTag, AddScript, or AddString on the $Sender->Head object to add stuff to the headmodule as it is being built.
For an example of this being done by us, look at the MobileThemeHooks class (themes/mobile/class.mobilethemehooks.php):/**
* Add mobile meta info. Add script to hide iphone browser bar on pageload.
*/
public function Base_Render_Before($Sender) {
if (IsMobile() && is_object($Sender->Head)) {
$Sender->Head->AddTag('meta', array('name' => 'viewport', 'content' => "width=device-width,minimum-scale=1.0,maximum-scale=1.0"));
$Sender->Head->AddString('
// If not looking for a specific comment, hide the address bar in iphone
var hash = window.location.href.split("#")[1];
if (typeof(hash) == "undefined") {
setTimeout(function () {
window.scrollTo(0, 1);
}, 1000);
}
');
}
}1
Answers
/applications/dashboard/views/default.master.php
Throw your code in right before the closing
tag.
A better way is to make a small plugin and hook into the Base_Render_Before event, then call AddTag, AddScript, or AddString on the $Sender->Head object to add stuff to the headmodule as it is being built.
For an example of this being done by us, look at the MobileThemeHooks class (themes/mobile/class.mobilethemehooks.php):
Vanilla Forums COO [GitHub, Twitter, About.me]
Thanks for the contribution @hyper, as it happens, I'm using the embed friendly theme.
Andy