Thursday, September 6, 2012

How to detect and save browser language preference into cookie?

    <script type="text/javascript">
        $(document).ready(function () {
            var c = $.cookie('userlanguage');
            var userLanguage = null;
            if (!c) {
                var language = window.navigator.userLanguage || window.navigator.language;
                $.cookie('userlanguage', userLanguage, { path: '/'});
            }
        });
    </script>

Wednesday, September 5, 2012

Setting cookies with jQuery



Set a cookie
Setting a cookie with jQuery is as simple as this, where a cookie is created called "example" with a value of "foo":

1
$.cookie("example", "foo");
This is a session cookie which is set for the current path level and will be destroyed when the user exits the browser. To make the same cookie last for e.g. 7 days do this:

1
$.cookie("example", "foo", { expires: 7 });
Note: the above examples will apply the cookie to the current path level.

If the page requested is http://www.example.com/file.html then it will be set for the / path and will be available for all paths at the domain. If the page is http://www.example.com/subpath/file.html then it will be set for the /subpath level and will not be accessible at / (or /otherpath etc).

To explicitly make the cookie available for all paths on your domain, make sure the path is set:

1
$.cookie("example", "foo", { path: '/' });
To limit it to a specific path instead:

1
$.cookie("example", "foo", { path: '/admin' });

Tuesday, September 4, 2012

How to detect browsers language preference?

By Javascript:
var language = window.navigator.userLanguage || window.navigator.language;

By check request header on server side:
Request.Headers["Accept-Language"]
Accept-Language:en-US;q=0.8,en;q=0.6

Scrum in 5 Minutes


ASP.NET MVC 4, Browser-Specific Views



private void registerDisplayMode(string agent, int index = 0)
{
DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode(agent)
{
ContextCondition = (context => context.GetOverriddenUserAgent().IndexOf
(agent, StringComparison.OrdinalIgnoreCase) >= 0)
});
}

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

registerDisplayMode("iPhone", 0);
registerDisplayMode("iPad", 1);
}