Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$(document).ready doesn't work under MVC4 project

I am just inserting

$(document).ready(function () {
    alert("!!!");
});

as usually into

@{
    ViewBag.Title = "Sign Up";
    Layout = "~/Views/Shared/_WebSite.cshtml";
}

<script type="text/JavaScript">

    $(document).ready(function () {
        alert("!!!");
    });

</script>

<h2>Sign Up</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
{

}

}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

but I cannot see alert() message...

other things of jQuery are working fine at this page...

What do I am missing?

Any clue?

Thank you!

like image 992
Friend Avatar asked Nov 10 '12 23:11

Friend


1 Answers

You can see jQuery is referenced after your code if you inspect the html.

Default project comes with an optional scripts section that will be rendered after jQuery reference in the layout, that's where your code should go.

@{
    ViewBag.Title = "Sign Up";
    Layout = "~/Views/Shared/_WebSite.cshtml";
}



<h2>Sign Up</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
{

}

}

@section Scripts {
    <script type="text/JavaScript">

    $(document).ready(function () {
        alert("!!!");
    });

    </script>
}
like image 188
Ufuk Hacıoğulları Avatar answered Oct 30 '22 18:10

Ufuk Hacıoğulları