js javascript browser language detection, get the user\'s browser\'s language preference setting using jquery to detect users br


/ Published in: jQuery
Save to your folder(s)

JQ - detect users browser language using http headers via ajax jsonp ajaxhttpheaders


Copy this code and paste it in your HTML
  1. /*
  2.  * 201008051404 - brandonjp
  3.  *
  4.  * I was using this...but it's bad (not accurate)....
  5.  
  6. // browser detect - returns the users browsers language preference
  7. function browsLang() {
  8. return ( navigator.language || navigator.userLanguage );
  9. };
  10.  
  11.  *
  12.  * Thanks to DanSingerman @ JavaScript for detecting browser language preference - Stack Overflow http://bit.ly/czTmtO
  13.  * This requires jQuery, but gives a more accurate read of the user's browser language pref
  14.  * by reading the http header from the user's browser upon request to : http://ajaxhttpheaders.appspot.com/
  15.  *
  16.  */
  17.  
  18. var browserLang;
  19.  
  20. jQuery.ajax({
  21. url: "http://ajaxhttpheaders.appspot.com",
  22. dataType: 'jsonp',
  23. success: function(headers) {
  24. browserLang = normaliseLang(headers['Accept-Language']);
  25. carryOnFriend();
  26. }
  27. });
  28.  
  29.  
  30. // The following function contains the real meat of the script file
  31. // But because I'm in a hurry, I'll wrap most of my file inside this function
  32. // Then we'll run this fn as a callback when everything else if finished
  33.  
  34. function carryOnFriend() {
  35.  
  36. // Everything else that needs to happen after the AJAX goes here
  37.  
  38. jQuery(document).ready(function($) {
  39. // Stuff to do as soon as the DOM is ready. Use $() w/o colliding with other libs;
  40. });
  41.  
  42. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.