In JSF - Getting the Client's Locale (To Display time in browser's timezone)

后端 未结 6 2115
失恋的感觉
失恋的感觉 2020-12-28 10:03

I\'m writing a webapp in JSF 2.0 that displays Timestamps as part of the information to the user. I would like the user to see the timestamps localized to their location (br

6条回答
  •  抹茶落季
    2020-12-28 10:57

    Another option would be to create a cookie on a Javascript that executes when the home page is ready. After that, the cookie will exist on each subsequent request and would be available

    Your Javascript could use jQuery and jsTimezoneDetect

        $(document).ready(function() {  
            setTimezoneCookie();
        });
    
        function setTimezoneCookie() {
    
            var timezone = jstz.determine().name();
    
            if (null == getCookie("timezoneCookie")) {
            document.cookie = "timezoneCookie=" + timezone;
            }
        }
    
        function getCookie(cookieName) {
            var cookieValue = document.cookie;
            var cookieStart = cookieValue.indexOf(" " + cookieName + "=");
            if (cookieStart == -1) {
                cookieStart = cookieValue.indexOf(cookieName + "=");
            }
            if (cookieStart == -1) {
                cookieValue = null;
            } else {
                cookieStart = cookieValue.indexOf("=", cookieStart) + 1;
                var cookieEnd = cookieValue.indexOf(";", cookieStart);
                if (cookieEnd == -1) {
                    cookieEnd = cookieValue.length;
                }
                cookieValue = unescape(cookieValue.substring(cookieStart, cookieEnd));
            }
            return cookieValue;
        }
    

    Your Facelet would then use the cookie's value if it exists:

    
       
       
    
    

提交回复
热议问题