Search code examples
asp.netjavascriptuser-agentauto-generate

ASP.net not generating javascript for some User Agents


********************Edit 2********************** I figured out the problem... But I don't like the implications. I was testing our iPhone targeted mobile application earlier and using a plugin to mask Firefox's User Agent String as an iPhone.

.Net was infact NOT generating the required code for post backs based on that piece of information alone.

I do not like this however, because since the iPhone and other multimedia devices can interpret javascript, ASP.net is breaking any application that relies on server generated javascript to run.

So, if the community will allow it... I'd like to change my official question to... Why will ASP.net not generate javascript for specific browsers and how can I turn this "feature" off.

*************** End Edit 2 ***************

I've got a weird problem. I copied some working code from my remote host to my computer at work. When I try to use the page I'm getting a javascript error

__doPostBack is not defined
javascript:__doPostBack('ctl00$ContentPlaceHolder1$login','')()()

When I few the output page source, sure enough there is no server side generated javascript.

I tried creating a simple page:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="jsTest.aspx.vb" Inherits="_jsTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="tbTest" runat="server"></asp:TextBox><br />
        <asp:LinkButton ID="linkTest" runat="server">LinkButton</asp:LinkButton>
    </form>
</body>
</html>

Codebehind:

Partial Class _jsTest
    Inherits System.Web.UI.Page
    Protected Sub linkTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles linkTest.Click
        Response.Write(tbTest.Text)
    End Sub
End Class

Getting the same error.

I've tried rebooting (hey, it works half the time), cleared out everything from App_Code, global.asax and web.config, added a textbox with autopostback=true... I'm out of ideas.

Can anyone shed some light on what's happening here?

**************More Information************** I just tried everything again in IE and it works as expected, the page source shows:

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTkxNTA2MDE2NWRkxhZMwlMVwJprcVsvQLJLrTcgaSM=" />

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>
<div>
    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwK20LZAAuzRsusGAsz0+6YPxxO+Ewv1XsD5QKJiiprrGp+9a3Q=" />
</div>

While the source in Firefox only shows:

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTkxNTA2MDE2NWRkxhZMwlMVwJprcVsvQLJLrTcgaSM=" />

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwK20LZAAuzRsusGAsz0+6YPxxO+Ewv1XsD5QKJiiprrGp+9a3Q=" />

Saving the web pages to the desktop and opening in notepad reveals the same thing...


Solution

  • The problem is the default way ASP.net treats unknown browsers... such as the iPhone. Even though it would be nice to assume unknown browsers could use javascript... you can specify what capabilities that a browser has in the section of web.config or machine.config.

    Check out http://slingfive.com/pages/code/browserCaps/ for an updated browsercaps config file for asp.net

    Here is an example of a case to match GECKO Based Browsers (Netscape 6+, Mozilla/Firefox, ...)

    <case match="^Mozilla/5\.0 \([^)]*\) (Gecko/[-\d]+)(?'VendorProductToken' (?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))?">
                    browser=Gecko
                    <filter>
                        <case match="(Gecko/[-\d]+)(?'VendorProductToken' (?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))">
                            type=${type}
                        </case>
                        <case> <!-- plain Mozilla if no VendorProductToken found -->
                            type=Mozilla
                        </case>
                    </filter>
                    frames=true
                    tables=true
                    cookies=true
                    javascript=true
                    javaapplets=true
                    ecmascriptversion=1.5
                    w3cdomversion=1.0
                    css1=true
                    css2=true
                    xml=true
                    tagwriter=System.Web.UI.HtmlTextWriter
                    <case match="rv:(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*))">
                        version=${version}
                        majorversion=0${major}
                        minorversion=0${minor}
                        <case match="^b" with="${letters}">
                            beta=true
                        </case>
                    </case>
                </case>