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.

How to insert a .js file to header?

edited March 2011 in Vanilla 2.0 - 2.8
How to insert a .js file to header?

or i want to insert some js code to header, how to do that?


  • Make a folder inside your plugins folder. Inside that new folder, make a file called default.php and put this in it: <?php if (!defined('APPLICATION')) die(); $PluginInfo['InsertSomeJS'] = array( 'Description' => 'Insert some JS', 'Version' => '0.1', 'Author' => 'Your Name', 'AuthorEmail' => '[email protected]', 'AuthorUrl' => '' ); class InsertSomeJS extends Gdn_Plugin { public function Base_Render_Before(&$Sender) { $Sender->AddJsFile($this->GetResource('myJSfile.js', FALSE, FALSE)); } public function Setup() {} }

    Now create myJSfile.js in the same directory and it will be added to the page head.
    Edit to your liking of course.
  • edited September 2010
    thx ithcy.

    but it is the only way to insert js or other code?
    If i just insert some script code, such as google analytics code, how to do that?
    I don't want to use the plugin because it use the old analytics code.

    thx again, i am a green hand in vanilla.
  • You're welcome. There's a plugin that uses the new analytics code.
  • thx, but i still want to know how to add script code or other code in header if it's not a analytics code?
    such as i want to add a simply navigation bar under the vanilla's header.

    thx again, you are a nice guy :-)
  • ithcyithcy New
    edited September 2010
    //This isn't what you want, but i'll leave it here in case someone wants to know how to add inline javascript.

    Do the same as above, but use this as default.php instead:<?php if (!defined('APPLICATION')) die(); $PluginInfo['InlineScript'] = array ( 'Name' => 'InlineScript', 'Description' => 'Adds script to head of all pages', 'Version' => '0.1', 'Author' => 'Your name', 'AuthorEmail' => '[email protected]', 'AuthorUrl' => '' ); class InlineScript extends Gdn_Plugin { public function Base_Render_Before(&$Sender) { $Sender->Head->AddString ( "<script type=\"text/javascript\">// your script goes here</script>" ); } public function Setup() {} }
  • Actually sorry, ignore that. I misunderstood what you wanted.
    What you want to do is create a theme. Take a look at for some help with that.
  • edited September 2010
    It doesn't work for me.

    I just want to add :

    <!--[if lte IE 8]> <link type="text/css" rel="stylesheet" href="styles-ie.css" /> <![endif]-->

    Any ideas ?
  • ithcyithcy New
    edited September 2010
    You should make a folder in your forum root called plugins/InlineScript.

    This folder should contain a file called default.php.

    That file should contain the code in this comment.

    You see where it says $Sender->Head->AddString ? You need to replace what is inside the "" with the code you want to add. $Sender->Head->AddString("<!--[if lte IE 8]>\n<link type=\"text/css\" rel=\"stylesheet\" href=\"styles-ie.css\" />\n<![endif]-->");
    You will need to go to your dashboard and enable the InlineScript plugin.
  • @ithcy

    It works! Thanks you very much ;)
  • Sure, glad I could help.
  • thx @ithcy

    the insert js to header is working now.
    but i still have a problems, if i want to insert a js file to footer, how to do it?

    thx again. :-)
  • AdrianAdrian Wandering Spirit Montreal MVP
    edited October 2010
    for the footer you can use the literal tag

    the code
Sign In or Register to comment.