Loading CSS Stylesheets Dynamically (Browser Evalution)
Load the Css Dynamically Based on the User's Browser
Myself, being a web programmer/developer I ran into the difficulty of making my css look consistent across browsers mostly because of the different
Tag rendering. So I finally reached the conclusion that it doesn't take much memory to load different css when the page first loads ,and the trip to a server wouldn't be that much expensive. So without any other boring stories here is one of the ways to determine what browser the user is using and loading the appropriate css. (demonstrated in C#.net ).
1.) In the section of your asp/aspx/ html page give your css stylesheet an id like so:
link id="myLinkstyle" runat="server" rel="Stylesheet" type="text/css">
Also make sure that you put runat="server". That will give you the opportunity to figure out the browser user is using from code behind the aspx page.
2.) In the Code behind, in your aspx.cs page or master.cs depending on design of your site enter the following code in your page_load method.
C#
protected void Page_Load(object sender, EventArgs e)
{
System.Web.HttpBrowserCapabilities browser = Request.Browser;
if (browser.Browser.ToLower() == "ie")
myLinkstyle.Href = "CSS/IEStyles.css";
else
myLinkstyle.Href = "CSS/OtherBrowserStyle.css";
}
That is the simplest thing you can do. The first line System.Web.HttpBrowserCapabilities browser = Request.Browser; just creates a browser object and assigns it to the request.browser property.
then if (browser.Browser.ToLower() == "ie") checks to see if the browser is of type IE. In that case it loads the IEstyles css other wise some other style.css
That is it.
The Request.Browser object has crapload of different properties with which you can make dynamic loading of javascript, css and other external
documents as customized as you want to. For instance you could get the type of the browser or its version by saying browser.GetType() or browser.Version.
Ok, Hope this short how to Tutorial helps someone. If you find any mistakes I made, please comment.
Published by LordFury
Hello peeps. I am a computer programmer, a martial artist a poker player and simply a good man who is fascinated with technology sports and how the world evolves on the daily bases! View profile
Where to Find Premade CSS CodesWriting CSS codes can take forever. If you have ever wanted to write CSS (cascading style sheets) faster, you have come to the right place. Even if you are just unsure of what k...- Top 3 Automatic CSS CreatorswWuldn't it be nice to have your initial CSS layout, CSS lists and CSS Navigation Elements automatically created using proper CSS design? Now you can with these 3 great tools!
- How to Create CSS Liquid LayoutsLiquid Layouts are used to create clean CSS designs that can expand or contract depending upon the browser size. They are becoming standard for a lot of new website designs, and Web 2.0 highly recommends the CSS liqui...
- How Make Your Website More Dynamic by Allowing User Input to Impact Your Content
- ASP.NET Blogging Software
- Improve Perfomance in ASP.Net
- Best Tutorials Books for Learning ASP.NET 2.0 And Visual Web Developer
- Getting Started with the .NET Framework
- The Best Tutorial Sites for Visual Basic.NET
- E-commerce Application Solutions Utilizing Microsoft's .NET





2 Comments
Post a CommentPerfect, just what I needed. Works great.
Great go buddy....
Cheers...!