Asp.net and JavaScript

This might sound silly but it is bugging me. I am learniing asp.net and I found out alot of calls which are done through a submit button uses a Javascript function. e.g. onLoad.

My issue is what happens when the browser has javascript disabled? I tried disabling javascript but the webforms work fine. how is this so?

Exactly, there are two different things going on here. There’s the .NET friendly way and the non-friendly way.

All the Javascripting you’re seeing (which is really badly written btw, that’s one of the things I don’t like.) won’t run if JS is off. However, HTML does recognize the <input type=“submit” /> element’s mandatory postback operation, that’s part of the (X)HTML, HTTP specs. So if the user has JS enabled, all the .NET code runs (and can perform client side validation, etc.), and if not only the postback occurs which sends the form data to the server where you do your server side validation and execute other server side code like the Page_Load function.

And good for you! I think MS would take more care of their output if more people like us were looking at View Source. :slight_smile:

The form will post the normal way if javascript is not enabled

Sorry I mean’t onClick events and postback. When I view the source code I see it created javascript within the head.


<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>



When JavaScript is disabled any scripts in the page don’t run. That’s why you need to make sure that the page will still work without JavaScript.

I am not 100% sure what your talking about, but it sounds like your talking about the page_load function. That function is server side. So when a form loads or posts back tp itself, it runs that function on server side before outputting it. It is not a javascript function