cookie实现的国际化
实例由三个文件组成:common.js?, parameter.js ,MyHtml.html代码如下:
?
common.js代码:
/*? 国际化? */
var currentLanguage= getCookie("choiceLanguage");
function typeChange(source){
? if(source == "zh")return 1;
? if(source == "en")return 2;
}
var strlanguage_s = new Array();
var strlanguage_en = new Array();
function langString(source){
? var choose = typeChange(currentLanguage);
? switch(choose){
??? case 1:return? strlanguage_s[source];
??? case 2:return? strlanguage_en[source];
??? default: alert("default");return strlanguage_s[source];
? }
? return? strlanguage_s[source];
}
//设置cookie
function setCookie(name, value) {??
??????
??? var exp = new Date();??
??? exp.setTime(exp.getTime() + (30*24*60*60*1000));??
??? window.document.cookie = name + "=" + escape (value)+ "; expires=" + exp.toGMTString()+"; path=/";??
}??
//取得cookie
function getCookie(name){??
??? var arg = name + "=";??
??? var alen = arg.length;??
??? var clen = window.document.cookie.length;??
??? var i = 0;??
??? while (i < clen) {??
??????? var j = i + alen;??
??????? if (window.document.cookie.substring(i, j) == arg)???
?????????? return getCookieVal (j);??
??????? i = window.document.cookie.indexOf(" ", i) + 1;??
??????? if (i == 0)???
????????? break;??
?? }??
?? return null;??
}??
//取出cookie的值??
function getCookieVal(offset) {??
??? var endstr = window.document.cookie.indexOf (";", offset);??
??? if (endstr == -1)???
???????? endstr = window.document.cookie.length;??
??? return unescape(window.document.cookie.substring(offset, endstr));??
}?
//删除cookie
function deleteCookie(name) {??
??? var exp = new Date();??
??? exp.setTime (exp.getTime() - 1);??
??? var cval = GetCookie (name);??
??? window.document.cookie = name + "=" + cval + "; expires=" +??? exp.toGMTString()+"; path=/";??
}?
?
=============================================
parameter.js代码:
var ipAddress=10;
var timeServerAddress=11;
strlanguage_s[ipAddress] = "IP地址";
strlanguage_s[timeServerAddress] = "服务器地址";
strlanguage_en[ipAddress] = "IP Address";
strlanguage_en[timeServerAddress] = "Address of time server";
?
================================================
MyHtml.html代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
??? <title>MyHtml.html</title>
???? <meta http-equiv="content-type" content="text/html; charset=UTF-8">
?????<script type="text/javascript" src="js/common.js"></script>
???? <script type="text/javascript" src="js/parameter.js"></script>
??? <script type="text/javascript">
??? function checkLanguage(){
????? var checks = document.getElementsByName("language");
????? var len = checks.length;
????? for(var i = 0; i < len; i++){
??????? if(checks[i].checked){
????????? setCookie("choiceLanguage",checks[i].value);
??????? }
????? }
????? window.location.reload();
??? }
???
??? </script>
? </head>
?
? <body>
??? 请选择语言: <br>
??? <input type="radio" name="language" value="zh" onclick="checkLanguage()"/>中文
??? <input type="radio" name="language" value="en" onclick="checkLanguage()"/>英文
??? <table border="1">
????? <tr>
??????? <td><script>document.write(langString(ipAddress))</script></td>
??????? <td><input type="text" name="ipAddress" id="ipAddress" value="192.168.2.69"></td>
????? </tr>
???? <tr>
??????? <td><script>document.write(langString(timeServerAddress))</script></td>
??????? <td><input type="text" name="timeServerAddress" id="timeServerAddress" value="www.baidu.com"></td>
????? </tr>
??? </table>
???
? </body>
</html>