Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

Curious Javascript in .NET

I’ve never had the opportunity nor the inclination to do anything with .NET; at work we use open source tools for all of our web development, and I prefer open source tools for my own personal experiments as well. At any rate, the javascript:__doPostBack links I’ve seen on .NET powered sites such as Channel 9 and Orkut plain give me the willies.

Anyway, I decided to view source and see what __doPostBack actually does. Here’s the function:

function __doPostBack(eventTarget, eventArgument) {
  var theform;
  if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
    theform = document._ctl0;
  }
  else {
    theform = document.forms["_ctl0"];
  }
  theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
  theform.__EVENTARGUMENT.value = eventArgument;
  theform.submit();
}

Note the use of a dubious user-agent based browser detection method. Now what I just don’t understand is why that browser detection is there in the first place. There are numerous ways of accessing a form by name using Javascript. document.forms['form-name'] is part of DOM level 0, and is supported by virtually every browser since Netscape 2. document.form-name is a Microsoft invention. The thing is, document.forms['form-name'] is supported by MS browsers as well. So why do a browser detect and use the MS only method when just using the other method with no detect would work equally as well?

This is Curious Javascript in .NET by Simon Willison, posted on 26th April 2004.

View blog reactions

Next: CSS-Discuss Wiki Spam

Previous: Kansas City web developer meetup

15 comments

  1. The ways of M$ are mysterious?

    <rant> If you, like me, run into a situation where you have to work with .NET, you'll see many strange things on your journey. Re-inventing the wheel to tie customers to one specific vendor / platform, has been common practice with M$ for years. </rant>

    Morgan Roderick - 26th April 2004 08:32 - #

  2. First of all, it was Netscape that added the document.formName syntax (as well as the corresponding shortcuts for the document.images and document.anchor syntaces), not Microsoft.

    Second, that syntax is supported by all browsers, but is not included in DOM1, which the DOM0 document.forms['formName'] syntax is.

    Third, Microsoft encourages developers to use the global namespace instead, so I don't see why they are still using document there, if they want to use the really proprietary way of referencing DOM objects.

    Fourth, what does .NET have to do with it? That's regular JavaScript code.

    liorean - 26th April 2004 11:11 - #

  3. """Fourth, what does .NET have to do with it? That's regular JavaScript code.""" It's the JavaScript which ASP.Net generates 'behind the scenes'. Perhaps this is just a side-effect of the way the 'generate JavaScript automatically' design works - code which frequently just detects IE and does nothing differently. Maybe in some scenarios specific browsers need other action as well?

    sfb - 26th April 2004 12:00 - #

  4. When ASP.Net first RTM'd, I spent a week trying to coerce "uplevel" behaviour to be cross-browser. I was sure it'd be worth-while, and there were many ways that ASP.Net could have had just a bit of wiggle room to make it possible; I eventually gave up, frustrated.

    I agree that this one in particular is pretty dubious, but that was the case I found in general. For example, many times, stock Validators could have done something cross-platform, but didn't.

    I realize that even M$ have limited resources, and they might have taken the uplevel=IE route to cut costs, but really-- the bang for the buck is pretty big for just debugging some stock javascripts.

    sfb's explanation of goofy code via generator sounds right on to me. I'd hate to think the developers of ASP+/ASP.Net would be that silly consciously.

    Jeremy Dunck - 26th April 2004 14:59 - #

  5. If Netscape invented the document.formName syntax, the decision to use browser detection and only use that method if microsoft is found in the user-agent is even weirder.

    Simon Willison - 26th April 2004 15:00 - #

  6. Oh-- while we're on the topic, it's worth noting that the next major release of ASP.Net is supposed to offer valid XHTML.

    Also, some discussion of working magic to make XHTML with ASP.Net 1.0.

    I don't have high hopes for cross-browser "uplevel" handling, though.

    Jeremy Dunck - 26th April 2004 15:06 - #

  7. You'd be surprized but we've had quite a few discussions about it at the ASP.NET Forums. In defence of XHTML I wrote an article on Producing XHTML-Compliant Pages With Response Filters and included a couple of links to those discussions. I agreee, the script above is a fudge and I chose to develop a workaround based on DOM. It does away with browser sniffing and references and refenreces the main form as follows:
    var theform = document.getElementById ('mainForm');
    It's been working on my site perfectly fine so far.

    Milan Negovan - 26th April 2004 17:01 - #

  8. Wow, imagine that an attack on Micorsoft from someone that believes software should be free, hmmm I am soo surprised.

    I am not sure why you would even bother scrutinizing the code of a product you say you don't care about.

    If you knew anything about .NET you would know that it allows you to replace any of the client side logic with your own stuff, so if you have that big of a problem with it you can replace it with something you feel works better.

    I find it down-right hypocritical when developers try to poke fun at how something was done when we are all human and I could imagine with some scrutiny of any developers code problems can and will be found.

    You act as if you are the all-knowing on the topic of javascript and how code should be written but you dont even post the entire document. There may be a reason that the code is written the way it was, perhaps they were solving a problem caused by one of the browsers they are trying to sniff out. My advice, stick to what you know [open source] and don't worry about products that you dont plan to use because you have political thought process.

    "Anyone that makes up their mind before understanding the issue is an idiot"
    -Chris Rock

    Netwalker - 28th April 2004 18:28 - #

  9. Netwalker: re-read my entry. I was careful not to brazenly bash Microsoft, much as I enjoy doing so. If you know why they use a browser detect in that code snippet (which stands on its own - the rest of the document has no baring on understanding it) then please tell me, and I'll post a retraction.

    Simon Willison - 28th April 2004 18:32 - #

  10. " re-read my entry. I was careful not to brazenly bash Microsoft, much as I enjoy doing so" That explains pretty much everything. It reminds me Slashdot idiots that also enjoy Microsoft bashing. They jump up and down like monkies, make up stories, act like Microsoft did something and then amuse themselves as well as those who watch them. Nice work Simon. I really like your Microsoft bashing, your denials make it even more enjoying.

    Alex - 1st May 2004 10:28 - #

  11. wtf? how did this debate about the technical idiocy of auto-generated javascript become an emotional flamefest? Of course no sane person would easily decide to replace part of WebForms even if possible just because it generates f*cked up javascript. Simply because the development and in particular the cost of testing that stuff would probably be too expensive.

    That said, the main problem is that unnecessary javascript is generated at all, which simply is not accessible and before you even dare to say anything: Microsoft wants it to be easy to create accessible websites. So whatever causes this piece of script is a bug and should be treated like one.

    jm - 2nd May 2004 03:29 - #

  12. Oddly enough, the browser detection code in .NET treats Mozilla as Netscape 4.x. Weird.

    Of course, last time I checked this was when I was using the first version of VS.NET, so when they released the update this may have changed.

    Brooks Williams - 5th May 2004 03:46 - #

  13. It seems you've snagged a couple of the trolls from the IEBlog. Alex Almeroth can't help but bring up Slashdot and open-source whenever he feels the need to spout gibberish at somebody.

    Jim - 16th January 2005 08:25 - #

  14. rjj

    yjyjyj - 2nd January 2006 10:46 - #

  15. Sir, Please specify the most important things in Java Script when we use this under ASP.Net. With Regards Tej Bahadur Singh 09879158582

    Tej - 10th April 2006 11:25 - #

Comments are closed.

Previously hosted at http://simon.incutio.com/archive/2004/04/26/dodgy

A django site