JavaScript Web page simplified and traditional conversion algorithm

Source: Internet
Author: User

Web page Special Effects page simplified and traditional conversion algorithm
var defaultencoding = 2; 1:
var translatedelay = 0;
var cookiedomain = "Http://www.111cn.net"; Change to your blog URL
var Msgtotraditionalchinese = "traditional"; Here you can change the text you want to display
var Msgtosimplifiedchinese = "Simplified"; Ditto, but neither of them suggests a change
var Translatebuttonid = "Translatelink";

var currentencoding = defaultencoding;
var Targetencodingcookie = "Targetencoding" + cookiedomain.replace (/./g, "");
var targetencoding = (GetCookie (targetencodingcookie) = = null? Defaultencoding:getcookie (Targetencodingcookie));
var translatebuttonobject;

function Translatetext (TXT)
{
if (txt = = "" | | txt = null)
Return "";
if (currentencoding = = 1 && targetencoding = 2)
return simplized (TXT);
else if (currentencoding = 2 && targetencoding = 1)
return traditionalized (TXT);
Else
return txt;
}

Function Translatebody (fobj)
{
 if (typeof (fobj) = = "Object")
  var Objs = Fobj.childno Des
 else
  var objs = document.body.childnodes;

 for (var i=0;i<objs.length;i++)
 {
  var obj=objs.item (i);
  if ( Obj.tagname = = "Object")
   continue
  if ("| | Br|hr|textarea| ". IndexOf ("|") +obj.tagname+ "|") > 0 | | obj = = translatebuttonobject)
   continue
  if (obj.title!= "" && obj.title! = null)
   obj.title = Translatetext (obj.title);
  if (Obj.alt!= "" && obj.al T!= null)
   obj.alt = Translatetext (Obj.alt);
  if (obj.tagname = "Input" &&am P Obj.value!= "" && obj.type!= "text" && obj.type!= "hidden")
   obj.value = Translat EText (Obj.value);
  if (obj.nodetype = 3)
   obj.data = Translatetext (obj.data);
  else
   translatebody (obj);
 }
}

function Translatepage ()
{
if (targetencoding = 1)
{
currentencoding = 1;
targetencoding = 2;
translatebuttonobject.innerhtml = Msgtotraditionalchinese;
Setcookie (Targetencodingcookie, targetencoding, 7);
Translatebody ();
}
else if (targetencoding = 2)
{
currentencoding = 2;
targetencoding = 1;
translatebuttonobject.innerhtml = Msgtosimplifiedchinese;
Setcookie (Targetencodingcookie, targetencoding, 7);
Translatebody ();
}
}

function Jtpystr ()
{
Return to ' simplified ';
}

function Ftpystr ()
{
return ' traditional ';
}

function traditionalized (CC)
{
var str = ';
var ss = Jtpystr ();
var tt = FTPYSTR ();

for (var i = 0; i < cc.length; i++)
{
if (Cc.charcodeat (i) > 10000 && ss.indexof (Cc.charat (i))!=-1)
str = Tt.charat (Ss.indexof (Cc.charat (i)));
Else
STR + + Cc.charat (i);
}
return str;
}

function simplized (cc) {
var str = ';
var ss = Jtpystr ();
var tt = FTPYSTR ();

for (Var i=0 i < cc.length; i++)
{
if (Cc.charcodeat (i) > 10000 && tt.indexof (Cc.charat (i))!=-1)
str = Ss.charat (Tt.indexof (Cc.charat (i)));
Else
STR + + Cc.charat (i);
}
return str;
}

function Setcookie (name,value,days)
{
if (days)
{
var date = new Date ();
Date.settime (Date.gettime () + (days*24*60*60*1000));
var expires = "; Expires= "+date.togmtstring ();
}
else var expires = "";
Document.cookie = name+ "=" +value+expires+ "; path=/";
}

function GetCookie (name)
{
var Nameeq = name + "=";
var ca = Document.cookie.split (';');
for (Var i=0;i < ca.length;i++)
{
var c = Ca[i];
while (C.charat (0) = = ') c = c.substring (1,c.length);
if (C.indexof (nameeq) = = 0) return c.substring (nameeq.length,c.length);
}
return null;
}

function Translateinitilization ()
{
Translatebuttonobject = document.getElementById (Translatebuttonid);
if (Translatebuttonobject)
{
With (Translatebuttonobject)
{
if (typeof (Document.all)!= "Object")
{
Href= "Javascript:translatepage ();";
}
Else
{
href= "#";
onclick = new Function ("Translatepage ()"; return false; ");
}
}

if (currentencoding!= targetencoding)
{
SetTimeout ("Translatebody ()", translatedelay);
if (targetencoding = 1)
translatebuttonobject.innerhtml = Msgtosimplifiedchinese;
Else
translatebuttonobject.innerhtml = Msgtotraditionalchinese;
}
}
}

Call method
<script type= "Text/javascript" >

var defaultencoding = 0;//default is traditional, 0-simplified, 1-traditional
var translatedelay = 0;//delay time, if not run at the end of the page, set the delay translation time, such as 100 for 100ms
var cookiedomain = "Http://www.111cn.net"; Cookie address, be sure to set, usually for your URL
var msgtotraditionalchinese = "Simplified-> traditional";////////////// Msgtosimplifiedchinese = "Traditional-> simplified"; The Chinese character
var translatebuttonid = "Translatelink";//Default interchange ID
translateinitilization ()
</script when the default switch is simplified "

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.