I just stumbled across a piece of sample code that I had written several months ago for a coworker, and I thought that I'd share it with everyone.
Here's the scenario: my coworker asked me if it was possible to have different client-side scripts inserted dynamically depending on the type of web browser that is being used. If the application was written in ASP.NET or some other dynamic language, then it would be trivial to determine the browser type and return the correct HTML <script>
block to the client. Unfortunately, he needed the script for both static and dynamic pages, and he didn't want to modify all of his pages unless absolutely necessary.
This sounded to me like a job for a pair of outbound rules in URL Rewrite.
With the above design requirements in mind, I wrote the following sample configuration for URL Rewrite that accomplishes the following tasks:
- If you’re using Internet Explorer, it inserts VBScript to display a popup message.
- If you’re not using Internet Explorer, it inserts JavaScript to display a different popup message.
In order to use this sample code, you need to open the web.config file for your application and add the following code for the URL Rewrite rules. (Note: You need to disable compression in order to avoid an HTTP 500.52 error, and the following sample code does just that.)
<system.webServer>
<rewrite>
<outboundRules>
<rule name="Add JavaScript" preCondition="IsNotInternetExplorer" patternSyntax="ExactMatch">
<match filterByTags="None" pattern="</body>" />
<action type="Rewrite" value="<script language="javascript">alert('You are not using Internet Explorer!');</script></body>" />
</rule>
<rule name="Add VBScript" preCondition="IsInternetExplorer" patternSyntax="ExactMatch">
<match filterByTags="None" pattern="</body>" />
<action type="Rewrite" value="<script language="vbscript">MsgBox "You are using Internet Explorer!"</script></body>" />
</rule>
<preConditions>
<preCondition name="IsInternetExplorer">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
<add input="{HTTP_USER_AGENT}" pattern=".*MSIE" negate="false" />
</preCondition>
<preCondition name="IsNotInternetExplorer">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
<add input="{HTTP_USER_AGENT}" pattern=".*MSIE" negate="true" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression doStaticCompression="false" doDynamicCompression="false" />
</system.webServer>
The above example is what I sent to my coworker, and I intended it as an easy place to start when you just want a simple chunk of script to be inserted. It works well, but a better example would be to have it dynamically insert code for an external script file based on the browser type. This is illustrated in the following example:
<system.webServer>
<rewrite>
<outboundRules>
<rule name="For Other Browsers" preCondition="IsNotInternetExplorer" patternSyntax="ExactMatch">
<match filterByTags="None" pattern="</body>" />
<action type="Rewrite" value="<script language="javascript" src="other.js"></script></body>" />
</rule>
<rule name="For Internet Explorer" preCondition="IsInternetExplorer" patternSyntax="ExactMatch">
<match filterByTags="None" pattern="</body>" />
<action type="Rewrite" value="<script language="javascript" src="msie.js"></script></body>" />
</rule>
<preConditions>
<preCondition name="IsInternetExplorer">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
<add input="{HTTP_USER_AGENT}" pattern=".*MSIE" negate="false" />
</preCondition>
<preCondition name="IsNotInternetExplorer">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
<add input="{HTTP_USER_AGENT}" pattern=".*MSIE" negate="true" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression doStaticCompression="false" doDynamicCompression="false" />
</system.webServer>
The above sample dynamically inserts an HTML <script>
block, and specifies one script file ("msie.js") for Internet Explorer a different script file ("other.js") for all other browsers.
A simple script for a simple task - just the way I like it. ;-]
Note: This blog was originally posted at http://blogs.msdn.com/robert_mcmurray/