Using jQuery from Orchard module page
Asked Answered
K

1

12

I'm trying to modify the Orchard.Search part with the search form and button to look and behave as I want. For this I need to use some jQuery features.

I added this to the header of the Search.SearchForm.cshtml file:

Script.Require("jQuery");

and I can see from the output of the page that jquery is added, at the bottom of the html, just before the ending tag:

<script src="/Orchard.Web/Modules/Orchard.jQuery/scripts/jquery-1.4.2.js" type="text/javascript"></script>

which looks fine. That's where the jQuery library is and I can download it from that location without probs. I've also added a small test-script in the page just to see if jQuery works properly:

<script language="javascript" type ="text/javascript">

$(document).ready(function () { 
  alert('page loaded');
});

</script>

But it's never fired and I get this script-error: Uncaught ReferenceError: $ is not defined

I'm getting tired of this, too much hassle but I guess I'm doing it all wrong...

Edit: Added the jquery tag and tried the suggestion answer about Script.Foot() which seems to work:

@using(Script.Foot()) {
    <script type ="text/javascript">
    //<![CDATA[
        $(document).ready(function () {
            alert('page loaded');
        });
    //]]>
    </script>
}
Kovrov answered 25/2, 2011 at 15:25 Comment(1)
Please, use "jquery" tag for questions relating to jQuery. Thanks.Rage
C
25

Well, that script of yours needs to appear after the inclusion of jQuery, otherwise $ is meaningless. You can add your script by surrouding it with @using(Script.Foot) { ...}:

@using(Script.Foot()) {
    <script type ="text/javascript">
    //<![CDATA[
    $(document).ready(function () { 
        alert('page loaded');
    });
    //]]>
    </script>
}
Clio answered 26/2, 2011 at 6:58 Comment(2)
Ofcourse, that's the problem. But the using(Script.Foot) code you suggested crashes with: CS1674: 'method group': type used in a using statement must be implicitly convertible to 'System.IDisposable' which is weird.Kovrov
Ah, got it working! The @using(Script.Foot()) was missing the parenthesis :). Super thanks for the help.Kovrov

© 2022 - 2024 — McMap. All rights reserved.