Posted By

brandonjp on 07/27/10


Tagged

get http ajax javascript js textmate headers browser jquery function detection settings to detect users language browsers preference using JSONP setting the fn via lang pref ajaxhttpheaders


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

seloh77


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


 / Published in: jQuery
 

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

  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  

You need to login to post a comment.