checkLanguage.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Created by chen on 2017/11/8.
  3. */
  4. !function () {
  5. var Skip={};
  6. //获取XMLHttpRequest对象(提供客户端同http服务器通讯的协议)
  7. Skip.getXmlHttpRequest=function (){
  8. if ( window.XMLHttpRequest ) // 除了IE外的其它浏览器
  9. return new XMLHttpRequest() ;
  10. else if ( window.ActiveXObject ) // IE
  11. return new ActiveXObject("MsXml2.XmlHttp") ;
  12. },
  13. //导入内容
  14. Skip.includeJsText =function (rootObject,jsText){
  15. if ( rootObject != null ){
  16. var oScript = document.createElement( "script" );
  17. oScript.type = "text/javascript";
  18. //oScript.id = sId;
  19. //oScript.src = fileUrl;
  20. //oScript.defer = true;
  21. oScript.text = jsText;
  22. rootObject.appendChild(oScript);
  23. //alert(oScript.text);
  24. }
  25. },
  26. //导入文件
  27. Skip.includeJsSrc =function (rootObject, fileUrl){
  28. if ( rootObject != null ){
  29. var oScript = document.createElement( "script" );
  30. oScript.type = "text/javascript";
  31. oScript.src = fileUrl;
  32. rootObject.appendChild(oScript);
  33. }
  34. },
  35. //同步加载
  36. Skip.addJs=function(rootObject, url){
  37. var oXmlHttp = Skip.getXmlHttpRequest() ;
  38. oXmlHttp.onreadystatechange = function(){//其实当在第二次调用导入js时,因为在浏览器当中存在这个*.js文件了,它就不在访问服务器,也就不在执行这个方法了,这个方法也只有设置成异步时才用到
  39. if ( oXmlHttp.readyState == 4 ){ //当执行完成以后(返回了响应)所要执行的
  40. if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 ){ //200有读取对应的url文件,404表示不存在这个文件
  41. Skip.includeJsSrc( rootObject, url);
  42. } else{
  43. // alert( 'XML request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')' ) ;
  44. }
  45. }
  46. }
  47. oXmlHttp.open('GET', url, false);
  48. oXmlHttp.send(null);
  49. Skip.includeJsText(rootObject,oXmlHttp.responseText);
  50. }
  51. var rootObject=document.head,url="";
  52. if($api.getStorage("languages")){
  53. switch ($api.getStorage("languages")){
  54. case "cn":
  55. url="language/cn.js";
  56. break;
  57. case "en":
  58. url="language/en.js";
  59. break;
  60. }
  61. }else {
  62. if (navigator.language.toLowerCase() == "zh-cn")
  63. {
  64. url="language/cn.js";
  65. }
  66. else if (navigator.language.toLowerCase() == "en-us")
  67. {
  68. url="language/en.js";
  69. }else
  70. {
  71. url="language/cn.js";
  72. }
  73. }
  74. if(document.head.getElementsByTagName("title")[0].innerText!="运力集"){
  75. url= "../"+url;
  76. }
  77. Skip.addJs(rootObject,url)
  78. }()