Adding a script from my themes in a widget... script loads before jQuery.

Topics: General
Mar 19, 2015 at 4:07 AM
Why when you add a script in the widget does it load before jQuery?

I have a script that relies on jQuery but it loads the line before!
<script src="/OrchardLocal/Themes/C3.CustomTheme/Scripts/my-script.js" type="text/javascript"></script>
<script src="/OrchardLocal/Modules/Orchard.jQuery/scripts/jquery-1.11.1.js" type="text/javascript"></script>
Mar 19, 2015 at 1:51 PM
use orchard way.
script.include("url");
Script.Require("prettyPhoto");
Mar 19, 2015 at 1:51 PM
use orchard way.
script.include("url");
Script.Require("prettyPhoto");
Developer
Mar 19, 2015 at 2:58 PM
If the script my-script.js specified in a resource manifest file? if so, you can set dependencies.
Mar 20, 2015 at 1:52 PM
Edited Mar 20, 2015 at 1:54 PM
If your script is included in a widget, it means that when n of those widgets are displayed the scripts also are loaded n times. Therefore it is recommended to do this the orchard way:

Create a class ResourceManifest with the following content:
public class ResourceManifest : IResourceManifestProvider
{
    public void BuildManifests(ResourceManifestBuilder builder)
    {
        var manifest = builder.Add();

        manifest.DefineScript("MyScript").SetUrl("my-script.min.js", "my-script.js").SetDependencies("jQuery");
    }
}
In your widget view add this:
@{
    Script.Require("MyScript"); // or Script.Require("MyScript").AtFoot() to place it before the body closing tag
}
Optioneel overig script:
@using (Script.Foot()) {
    <script type="text/javascript">
        // do stuffs
    </script>
}