where do I put a script file for a specific page, widget, or content type

Topics: Customizing Orchard, Troubleshooting, Writing modules
Apr 8, 2011 at 12:54 PM

I'd like to include a javascript file for a page I've added but I'm having a hard time figuring out where I should put that file.  Do I really need to create module just to include one javascript file?  Should I just add it to the theme?

Also, it feels kinda hacky to put a script tag in the html editor markup for the page.  What if I wanted to include it in the <head> tag or added to the tail? 

It would be nice to have a place to put some custom resource that doesn't require compilation.  Maybe a static content type.  Is that already in there?  am I missing something?

Apr 8, 2011 at 2:59 PM

A good place to put a Javascript file is in a "scripts" subfolder of the theme you are using.  See the doc "Anatomy of a Theme" (http://orchardproject.net/docs/Anatomy-of-a-theme.ashx) which discusses how to include static resources like CSS and script files with a theme.

Apr 8, 2011 at 4:39 PM

It's not super-clean but having put the script in your Scripts folder for the theme you can use Layout.cshtml to control which scripts get loaded and you can use Request.Path to control the pages they show up on.  For example, if you had a control that only appears on the home page of your site you could put this in Layout.cshtml in your theme:

@{

    if (Request.Path == "/")
    {
        Style.Include("mastheadstyle.css");
        Script.Include("masteheadscript.js");
    }
}

This at least eliminates any need to put script tags into the body of any page and you have full control over Head, Footer, UseCdn etc..

Apr 8, 2011 at 5:11 PM

excellent, thx!