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.
Options

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?
Tagged:

Comments

  • Options
    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' => 'your@email.com', 'AuthorUrl' => 'http://example.com' ); 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.
  • Options
    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.
  • Options
    You're welcome. There's a plugin that uses the new analytics code.
    http://www.vanillaforums.org/addon/608/analytics
  • Options
    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 :-)
  • Options
    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' => 'your@email.com', 'AuthorUrl' => 'http://example.com' ); 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() {} }
  • Options
    Actually sorry, ignore that. I misunderstood what you wanted.
    What you want to do is create a theme. Take a look at http://www.vanillaforums.org/page/ThemeQuickStart for some help with that.
  • Options
    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 ?
  • Options
    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.
  • Options
    @ithcy

    It works! Thanks you very much ;)
  • Options
    Sure, glad I could help.
  • Options
    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. :-)
  • Options
    AdrianAdrian Wandering Spirit Montreal MVP
    edited October 2010
    for the footer you can use the literal tag

    {literal}
    the code
    {/literal}
Sign In or Register to comment.