Trying to understand the sequence of events when a master page, partials, and JavaScript code are involved.
Imagine having a Master page with scripts:
<%@ Master Language="C#" ... %>
<head>
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
// header content
</head>
<body>
// HTML content here
</body>
<script type="text/javascript">
function foo () {
};
</script>
Now, let's look at a page, Index.aspx, that inherits from this Master page and contains:
<script type="text/javascipt">
foo();
</script>
The issue arises where Chrome's console displays an error stating "foo() is not defined"
. Moving the script from the Master page to its header resolves this problem.
So, some questions arise -
What occurs first in terms of order? Does Index.aspx begin rendering before the Master page fully loads, causing it not to recognize what's defined at the bottom of the Master page (and works fine if positioned at the top)? Yes, I believe so.
A more intriguing question - If the server-side rendering of the Master page isn't complete yet, how does JavaScript from Index.aspx get triggered? Simply put, if we have pages A rendering B rendering C, which JavaScript gets called first - the one on C or the last one being called?