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?
<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 - #
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 - #
sfb - 26th April 2004 12:00 - #
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 - #
Simon Willison - 26th April 2004 15:00 - #
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 - #
var theform = document.getElementById ('mainForm');It's been working on my site perfectly fine so far.Milan Negovan - 26th April 2004 17:01 - #
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 - #
Simon Willison - 28th April 2004 18:32 - #
Alex - 1st May 2004 10:28 - #
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 - #
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 - #
Jim - 16th January 2005 08:25 - #
yjyjyj - 2nd January 2006 10:46 - #
Tej - 10th April 2006 11:25 - #