Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force ASP.NET to generate JavaScript for all User Agents

I noticed recently in my ASP.NET web application that if I set my User Agent to an empty string (using a FireFox plug-in to spoof the user agent), then ASP.NET will not generate the javascript required to perform postbacks. More specifically, if you try calling the __doPostBack(a, b) function from your javascript, you will get an error saying that function is undefined.

I understand that every browser has a user agent, so this won't come up that often, but the essence of the problem still exists: there are cases in which an unrecognized or malformed user agent can render your web application unusable if you rely on postbacks.

This is similar to this question: ASP.net not generating javascript for some User Agents, but if I'm reading it right it looks like you'd fix each unrecognized user agent case by case and mask it as another browser. My concern is less with an individual user agent, and more so with the overall fact that certain user agents won't be able to use my application and I won't know it because the error happens in javascript and not on the server.

Does anyone know of a way that I can force ASP.NET to always generate the required javascript?

like image 604
Adam Avatar asked Dec 24 '12 21:12

Adam


People also ask

How to implement JavaScript in ASP NET?

Implementing JavaScript 1 OnClientClick. The first catch that comes with using JavaScript with ASP.NET is that both languages have an OnClick event for buttons and ASP’s event takes precedence. 2 ClientID. The getElementById function in JS is used to get and change values in controls such as text boxes. ... 3 Other ASP.NET properties. ...

How do I get Started with user agents?

The fastest way to get started is to hop down to the Examples section where you can see it in action! The User Agents package is available on npm with the package name user-agents . You can install it using your favorite JavaScript package manager in the usual way.

How do I use the user-agents library?

The User-Agents library offers a very flexible interface for generating user agents. These examples illustrate some common use cases, and show how the filtering API can be used in practice. The most basic usage involves simply instantiating a UserAgent instance. It will be automatically populated with a random user agent and browser fingerprint.

How do I change the user agent to a string?

The userAgent.toString () call converts the user agent into a string which corresponds to the actual user agent. The data property includes a randomly generated browser fingerprint that can be used for more detailed emulation. By passing an object as a filter, each corresponding user agent property will be restricted based on its values.


Video Answer


1 Answers

If you set empty to the User Agent, or if you use an unknown User Agent string, then asp.net reads the Default.browser file from Browsers directories and there you can define how to you like to act on this cases.

The lines that you need to change and on this cases use javascript is that ones:

        <capability name="jscriptversion"       value="5.6" />
        <capability name="javascript"           value="true" />
        <capability name="javascriptversion"    value="1.5" />

on the default, the javascript line if false. I like to comment here that I am not so sure if you really need to change it, if some one spoof the user agent, let it do it. If he likes your site and need to use it, then is better let you know how to handle the browser that he use. From the other hand you must take care if this is possible, your site to work even with out javascript, at least for most of the actions you do.

Is better to avoid link buttons (that use the _doPostBack) and use post back buttons for example. I know that GridView and other use also the _doPostBack for paging... ok with that, but is better one good site to been able to work even with out javascript.

Some similar question:
__doPostBack is undefined on DotNetNuke website for IE 10

and this blog: Bug and Fix: ASP.NET fails to detect IE10 causing _doPostBack is undefined JavaScript error or maintain FF5 scrollbar position

like image 145
Aristos Avatar answered Oct 26 '22 23:10

Aristos