How to use javascript(js file) in Orchard CMS

user518363 picture user518363 · Aug 22, 2011 · Viewed 13.3k times · Source

Can any one tell me please how to use js file in Orchard CMS? I added it to Layout.cshtml page as Script.include("jquery.js").

Answer

Piotr Szmyd picture Piotr Szmyd · Aug 22, 2011

You can do it twofold.

First approach is just like mdm described:

  1. Create your own implementation of IResourceManifestProvider interface (take a look at Orchard source - it's been implemented in many modules), implement BuildManifests(ResourceManifestBuilder builder) method and create a named resource for a given .js file
  2. Use @{ Script.Require("[your resource name]"); } in your Razor view file (.cshtml) to include that script.

This is a preferred solution if you have many script files, possibly with dependencies between them. It allows you to specify the dependencies for each script file and make Orchard take care of the rest (so when you reference a given resource, all dependent ones would also be automatically referenced in the proper order).

The second, simpler approach is to directly reference the .js script file in your .cshtml file, without creating a named resource. It's useful if you'd like to quickly add a reference to a single script. Like this (example taken from Orchard.Web\Core\Shapes\Views\Document.cshtml):

@{ Script.Include("html5.js").AtLocation(ResourceLocation.Head); }