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

apbarrattapbarratt New
edited June 2011 in Vanilla 2.0 - 2.8
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:

Best Answer

  • TimTim Operations Vanilla Staff
    Answer ✓
    If you're running the default Vanilla theme you can access the header HTML here:

    /applications/dashboard/views/default.master.php

    Throw your code in right before the closing </head> tag.
    This is the worst way to approach this task I'm afraid. When Vanilla is updated, your code vanishes or causes conflicts.

    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);
    }
    ');
    }
    }

    Vanilla Forums COO [GitHub, Twitter, About.me]

Answers

  • If you're running the default Vanilla theme you can access the header HTML here:

    /applications/dashboard/views/default.master.php

    Throw your code in right before the closing tag.
  • TimTim Operations Vanilla Staff
    Answer ✓
    If you're running the default Vanilla theme you can access the header HTML here:

    /applications/dashboard/views/default.master.php

    Throw your code in right before the closing tag.
    This is the worst way to approach this task I'm afraid. When Vanilla is updated, your code vanishes or causes conflicts.

    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);
    }
    ');
    }
    }

    Vanilla Forums COO [GitHub, Twitter, About.me]

  • Looks good @Tim, I shall try that, I discovered the joys of overwriting work with updates yesterday, silly me XD

    Thanks for the contribution @hyper, as it happens, I'm using the embed friendly theme.

    Andy
Sign In or Register to comment.