Razor generates jQuery too late for a custom script using it despite Scripts.Render order

I asked why below does not work and got a very clear answer .

<head>
  ...
  <script src="Stuff.js" type="text/javascript"></script>
</head>
<body>
  ...
  @Scripts.Render("~/bundles/jquery")
</body>

So, I changed it to this.

<head></head>
<body>
  @RenderBody()
  @Scripts.Render("~/bundles/jquery")
  @Scripts.Render("~/Stuff.js")
</body>

However, I get the same wrong behavior. Based on the diagnostics from the linked answer, I assume that the scripts are not yet displayed in the correct order. How do I control this? Should I move both headers instead?

The generated markup ends as follows.

      </form>
    </div>
    <script src="/Scripts/jquery-1.10.2.js"></script>
    <script src="Stuff.js"></script>
  </body>
</html>

As set, I am adding a screenshot of a network tab showing scripts. There are other resources (images, etc.), but none of them are 4xx, no strange messages, and I disabled them, still encountering the same problem.

enter image description here

Default.js( Stuff.js).

window.onload = function() {
  console.log($(this));
};

Default.js( ).

//$(document).ready(function () { alert("ready"); });
$(window).onload(function () { alert("onload"); });
+4
1

$(window).load(function () { alert("onload"); });

onload - JavaScript, JQuery

: $(window).load(function() { $(document).ready(function() {

, ,

 @RenderBody()
 @Scripts.Render("~/bundles/jquery")
 @Scripts.Render("~/Stuff.js")

- JQuery, , - , JQuery . JQuery RenderBody

, JQuery

:

@RenderBody()
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/Stuff.js")
@RenderSection("Scripts", required: false) 

javascripts

@section Scripts {
<script type="text/javascript">
        //<![CDATA[
        $(document).ready(function() { alert ('Hi'); });
        //]]>
</script>
}

,

+1

Source: https://habr.com/ru/post/1622759/


All Articles