I am trying to move all my javascript to the bottom of the page. I am using MVC with Razor. I wrote a helper method for registering scripts. It stores scripts in the order in which they are registered, and eliminates duplication.
@{ Html.RegisterScript("/scripts/someFile.js"); }
And then right before the closing body tag, I do something like this:
@Html.RenderScripts() </body>
All this works fine if the scripts are registered on one page. If I register some scripts in my layout and some scripts in the internal representation, everything will go wrong. Basically, the internal view is performed before the layout. Thus, the event, although the scripts in the internal representation depend on the scripts in the external representation, they are registered earlier.
So, if I have _Master.cshstml and it has
@{ Html.RegisterScript("script1.js"); Html.RegisterScript("script2.js"); }
And then I have InnerView.cshtml with _Master.cshtml as its layout, and in the view I have
@{ Html.RegisterScript("script3.js"); }
The scripts are obtained in the following order: script3, script1, script 2.
Is there any way to get the layout to execute before the internal representation? I tried moving things everywhere, and everything that seems internal always seems to be the first. Perhaps this is expected? If so, is it best for me to accomplish what I'm trying to do?