/ Published in: jQuery
JQ - detect users browser language using http headers via ajax jsonp ajaxhttpheaders
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* * 201008051404 - brandonjp * * I was using this...but it's bad (not accurate).... // browser detect - returns the users browsers language preference function browsLang() { return ( navigator.language || navigator.userLanguage ); }; * * Thanks to DanSingerman @ JavaScript for detecting browser language preference - Stack Overflow http://bit.ly/czTmtO * This requires jQuery, but gives a more accurate read of the user's browser language pref * by reading the http header from the user's browser upon request to : http://ajaxhttpheaders.appspot.com/ * */ var browserLang; jQuery.ajax({ url: "http://ajaxhttpheaders.appspot.com", dataType: 'jsonp', success: function(headers) { browserLang = normaliseLang(headers['Accept-Language']); carryOnFriend(); } }); // The following function contains the real meat of the script file // But because I'm in a hurry, I'll wrap most of my file inside this function // Then we'll run this fn as a callback when everything else if finished function carryOnFriend() { // Everything else that needs to happen after the AJAX goes here jQuery(document).ready(function($) { // Stuff to do as soon as the DOM is ready. Use $() w/o colliding with other libs; }); }