Here's a basic example of what's happening. I can understand that I'm probably calling the function in body before it's defined but then why is it so common to put javascript at the bottom of the page? I know my boss is going to say use bottom javascript so what should I do?

Also the root issue is when the page is first loaded, if there is a username from the server I need to run the CheckUsername() function. So is there a better way to do it conditionally?

<body>
    <input type="text" name="username" 
        id="username" onblur="CheckUsername()" 
        maxlength="30" value="@username"  />

    @if (username.length > 5) 
    {
         <script>
            CheckUsername(); // this is the one that's undefined
         </script>
    }  

    @section bottomjavascript
    {
        <script language="JavaScript">

            function CheckUsername() {
                // does work
            }

        </script>
    }


</body>

The actual body tags come from the master layout. But why can I use CheckUsername in the input tag, but I can't just call it on the page?

Related posts

Recent Viewed