1

I have a Razor view containing this script:

<script type="text/javascript">
    jQuery(function()
    {
        alert("hello world");
    })
</script>

The script doesn't open an alert Dialog when the page is loaded.

Portion of the BundleCfg:

bundles.Add(New ScriptBundle("~/bundles/jquery").Include(
            "~/Scripts/jquery-{version}.js",
            "~/Scripts/jquery.unobtrusive-ajax.js",
            "~/Scripts/jquery.validate.unobtrusive.js"))

bundles.Add(New ScriptBundle("~/bundles/jqueryval").Include(
            "~/Scripts/jquery.validate*"))

At the End of the Layout Body:

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required:=False)
5
  • there must be some problem with your registration of your bundle. Commented Nov 16, 2016 at 13:11
  • Could you specifiy the Problem please? Commented Nov 16, 2016 at 13:12
  • <script src="code.jquery.com/jquery-3.1.1.min.js" ></script> add this in your html file Commented Nov 16, 2016 at 13:14
  • You rely on the webservice with this Approach. Commented Nov 16, 2016 at 13:15
  • What about this (function($) { $(document).ready(function() { // Code }); })(jQuery); Commented Nov 16, 2016 at 13:21

3 Answers 3

2

Try this

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

here is details: https://learn.jquery.com/using-jquery-core/document-ready/

Acording to link on top I realized that your code:

<script type="text/javascript">
    jQuery(function()
    {
        alert("hello world");
    });
</script>    

is the same as this

<script type="text/javascript">
  $(document).ready(function() {
      alert("hello world");
  });
</script>

So problem was just about place where to render bundles.

Sign up to request clarification or add additional context in comments.

1 Comment

You should to declare @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryval") @Scripts.Render("~/bundles/bootstrap") before your JS. I put it in <head> tag of layout
2

The problem is that the jQuery library is loaded after the symbol $ (jQuery) is called; that's why is doesn't work.

You have to put the line:

@Scripts.Render("~/bundles/jquery")

before the body is rendered.

Comments

0

Change your code with this.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.