Posted in Javascript onMarch 12, 2014
公司打算进入台湾市场,最近开发了繁体版本的网站,数据库里的信息全是简体,除了网页上固定的文字手动翻译了,文章内容标题都不是繁体。
于是在网上找了一段比较流行的繁简切换的JS实现了,不过后来却发现,有些页面会卡死,根本无法执行下去。弹出信息表明都是这个繁简切换的JS带来的问题。
仔细查看了下,发现原来是这个问题。分享下,也许有不少人遇到类似的问题。
先上代码:
//模仿语言包式的简繁转换功能插件! var Default_isFT = 0 //默认是否繁体,0-简体,1-繁体 var StranIt_Delay = 50 //翻译延时毫秒(设这个的目的是让网页先流畅的显现出来) //-------代码开始,以下别改------- //转换文本 function StranText(txt,toFT,chgTxt) { if(txt==""||txt==null)return "" toFT=toFT==null?BodyIsFt:toFT if(chgTxt)txt=txt.replace((toFT?"简":"繁"),(toFT?"繁":"简")) if(toFT){return Traditionalized(txt)} else {return Simplized(txt)} } //转换对象,使用递归,逐层剥到文本 function StranBody(fobj) { if(typeof(fobj)=="object"){var obj=fobj.childNodes} else { var tmptxt=StranLink_Obj.innerHTML.toString() if(tmptxt.indexOf("简")<0) { BodyIsFt=1 StranLink_Obj.innerHTML=StranText(tmptxt,0,1) //StranLink.title=StranText(StranLink.title,0,1) } else { BodyIsFt=0 StranLink_Obj.innerHTML=StranText(tmptxt,1,1) //StranLink.title=StranText(StranLink.title,1,1) } setCookie(JF_cn,BodyIsFt,7) var obj=document.body.childNodes } for(var i=0;i<obj.length;i++) { var OO=obj.item(i) if("||BR|HR|TEXTAREA|".indexOf("|"+OO.tagName+"|")>0||OO==StranLink_Obj)continue; if(OO.title!=""&&OO.title!=null)OO.title=StranText(OO.title); if(OO.alt!=""&&OO.alt!=null)OO.alt=StranText(OO.alt); if(OO.tagName=="INPUT"&&OO.value!=""&&OO.type!="text"&&OO.type!="hidden")OO.value=StranText(OO.value); if(OO.nodeType==3){OO.data=StranText(OO.data)} else StranBody(OO) } } function JTPYStr() { return '皑蔼碍爱翱袄奥坝罢摆败颁办绊帮绑镑谤剥饱宝报鲍辈贝钡狈备惫绷笔毕毙闭边编贬变辩辫鳖瘪濒滨宾摈饼拨钵铂驳卜补参蚕残惭惨灿苍舱仓沧厕侧册测层诧搀掺蝉馋谗缠铲产阐颤场尝长偿肠厂畅钞车彻尘陈衬撑称惩诚骋痴迟驰耻齿炽冲虫宠畴踌筹绸丑橱厨锄雏础储触处传疮闯创锤纯绰辞词赐聪葱囱从丛凑窜错达带贷担单郸掸胆惮诞弹当挡党荡档捣岛祷导盗灯邓敌涤递缔点垫电淀钓调迭谍叠钉顶锭订东动栋冻斗犊独读赌镀锻断缎兑队对吨顿钝夺鹅额讹恶饿儿尔饵贰发罚阀珐矾钒烦范贩饭访纺飞废费纷坟奋愤粪丰枫锋风疯冯缝讽凤肤辐抚辅赋复负讣妇缚该钙盖干赶秆赣冈刚钢纲岗皋镐搁鸽阁铬个给龚宫巩贡钩沟构购够蛊顾剐关观馆惯贯广规硅归龟闺轨诡柜贵刽辊滚锅国过骇韩汉阂鹤贺横轰鸿红后壶护沪户哗华画划话怀坏欢环还缓换唤痪焕涣黄谎挥辉毁贿秽会烩汇讳诲绘荤浑伙获货祸击机积饥讥鸡绩缉极辑级挤几蓟剂济计记际继纪夹荚颊贾钾价驾歼监坚笺间艰缄茧检碱硷拣捡简俭减荐槛鉴践贱见键舰剑饯渐溅涧浆蒋桨奖讲酱胶浇骄娇搅铰矫侥脚饺缴绞轿较秸阶节茎惊经颈静镜径痉竞净纠厩旧驹举据锯惧剧鹃绢杰洁结诫届紧锦仅谨进晋烬尽劲荆觉决诀绝钧军骏开凯颗壳课垦恳抠库裤夸块侩宽矿旷况亏岿窥馈溃扩阔蜡腊莱来赖蓝栏拦篮阑兰澜谰揽览懒缆烂滥捞劳涝乐镭垒类泪篱离里鲤礼丽厉励砾历沥隶俩联莲连镰怜涟帘敛脸链恋炼练粮凉两辆谅疗辽镣猎临邻鳞凛赁龄铃凌灵岭领馏刘龙聋咙笼垄拢陇楼娄搂篓芦卢颅庐炉掳卤虏鲁赂禄录陆驴吕铝侣屡缕虑滤绿峦挛孪滦乱抡轮伦仑沦纶论萝罗逻锣箩骡骆络妈玛码蚂马骂吗买麦卖迈脉瞒馒蛮满谩猫锚铆贸么霉没镁门闷们锰梦谜弥觅绵缅庙灭悯闽鸣铭谬谋亩钠纳难挠脑恼闹馁腻撵捻酿鸟聂啮镊镍柠狞宁拧泞钮纽脓浓农疟诺欧鸥殴呕沤盘庞国爱赔喷鹏骗飘频贫苹凭评泼颇扑铺朴谱脐齐骑岂启气弃讫牵扦钎铅迁签谦钱钳潜浅谴堑枪呛墙蔷强抢锹桥乔侨翘窍窃钦亲轻氢倾顷请庆琼穷趋区躯驱龋颧权劝却鹊让饶扰绕热韧认纫荣绒软锐闰润洒萨鳃赛伞丧骚扫涩杀纱筛晒闪陕赡缮伤赏烧绍赊摄慑设绅审婶肾渗声绳胜圣师狮湿诗尸时蚀实识驶势释饰视试寿兽枢输书赎属术树竖数帅双谁税顺说硕烁丝饲耸怂颂讼诵擞苏诉肃虽绥岁孙损笋缩琐锁獭挞抬摊贪瘫滩坛谭谈叹汤烫涛绦腾誊锑题体屉条贴铁厅听烃铜统头图涂团颓蜕脱鸵驮驼椭洼袜弯湾顽万网韦违围为潍维苇伟伪纬谓卫温闻纹稳问瓮挝蜗涡窝呜钨乌诬无芜吴坞雾务误锡牺袭习铣戏细虾辖峡侠狭厦锨鲜纤咸贤衔闲显险现献县馅羡宪线厢镶乡详响项萧销晓啸蝎协挟携胁谐写泻谢锌衅兴汹锈绣虚嘘须许绪续轩悬选癣绚学勋询寻驯训讯逊压鸦鸭哑亚讶阉烟盐严颜阎艳厌砚彦谚验鸯杨扬疡阳痒养样瑶摇尧遥窑谣药爷页业叶医铱颐遗仪彝蚁艺亿忆义诣议谊译异绎荫阴银饮樱婴鹰应缨莹萤营荧蝇颖哟拥佣痈踊咏涌优忧邮铀犹游诱舆鱼渔娱与屿语吁御狱誉预驭鸳渊辕园员圆缘远愿约跃钥岳粤悦阅云郧匀陨运蕴酝晕韵杂灾载攒暂赞赃脏凿枣灶责择则泽贼赠扎札轧铡闸诈斋债毡盏斩辗崭栈战绽张涨帐账胀赵蛰辙锗这贞针侦诊镇阵挣睁狰帧郑证织职执纸挚掷帜质钟终种肿众诌轴皱昼骤猪诸诛烛瞩嘱贮铸筑驻专砖转赚桩庄装妆壮状锥赘坠缀谆浊兹资渍踪综总纵邹诅组钻致钟么为只凶准启板里雳余链泄台标'; } function FTPYStr() { return '????勐O??W?瘟T?[?☆C???徒?????????篚U??????v????篱]?????????l?I??P??芾??????⑿Q???M?K?N???}?????y?釉??v?较s?????????L?????S?斥n???m???畏Q?驼\?????u???_??????????N????τ|??鳢?????????????????????зJ??梧??勰????????觞h??n?v?u??ПI?羿???爝f???|??蔗??叠???????|????鲷Y?侏??????嗑??蛾???????Z????吼I????????????┕???????U?????^?????麂h?????????彷o?????D?????众s????????????R????????m????????蛐M???????T??V?矽?w????????]??L???^???h????M???後?刈o???W????????g????Q?警???o???]??зV???Z?≈M?????夥?????C???????O???D?姿E?????????A?????r?????怨{?????z?A???旌????p??????????︷T?u?R??{????????豺???????e????????????????蒋d??Q???f???????※N???????镁o??H???x?a??徘G??Q???????P??ふn????煅???K?~?????r??h????U?????碣????r??????????欣|???E????疯D?绢??I?????????畹[??r??z?????z?i??磕?????????奢v?????????C????R??X???????祷\??n????Шt????]?t?稃u????????武X?H?铱|?]?V??n???\??y?噍?????S??????????????????豳I????????M?????麽??]????????糁i??????R?????????????夏X?吏[???f?俞?????????甯?Q?翕o???廪r???W???I?a????圪r?????????{?????滗??阕V????????????L??????????\??q??????????????S????J????A???c????^???????s????_??犴g???s???????⑺_????黍}???⒓???耖W?????p??B??z?卦O????鹉I?B???俾}????裨???r??????葆?????郢F?休???H?傩g?湄Q??????????q????Z????\??????q?O?p???????E???????T??U??C??l???????l???d??N????D?T?F???????E?????愁B????????H????^????芈???????胛??u??桄u?跽_?o???]??照`??抟u???蚣????{?b??B????????????????????????????[??f??y?????a?????扮n???u??????疫x???W?煸??ゑZ????壶f??????????李???????┲V????P?????蝇??u?蜻b???????I??????x?は???|???????????????膪???t???I?上?????虬b??????n????????O?逝c?Z?????????Y??@?T?A???????[???????螂E????????妮d???嘿??????????t?少\???????????直K?剌????鹁`???q?べ~?????????稍\???瓯?????????碳???S?觅|???????????????T??谫A????4u???肚f??y??铄F??????崞???n????????????殡b???㈤???????_??; } function JTPYWord(){ var jtpyword = new Array('“','”','‘',''','三?O管','三极管' ,'中文?','串行','串列加速器','以太网','奶酪','二?O管','二极管','交互式','阿塞拜疆','人工智能','接口','任意球?T','任意球员', '服务器','字?','字节','优先级','元??,'元凶','光盘','光驱','克?地??','克罗地亚','全角','冬天?','冬日?','凉菜','冷菜', '凶器','?雌?,'凶徒','?赐?,'?词?,'凶手','?窗?,'凶案','凶??','???','凶残','???,'凶杀','凶??,'分布式','打印','列支敦士登','剪彩', '加蓬','总线','局域网','特立尼?和多巴哥','特立尼达和托巴哥','半角','卡塔??','卡塔尔','打印?C','打印机','厄立特里??','厄立特里亚','厄瓜多尔','厄瓜多??', '斯威士兰','斯威士?','吉布提','吉布堤','基里巴斯','?D瓦?','图瓦卢','哈萨克斯坦','哥斯?黎加','哥斯达黎加','格?吉??','格鲁吉亚','佐治亚','佐治??','嘴?', '土库曼斯坦','薯仔','土豆?','土豆网','坦桑尼亚','坦桑尼??','端口','塔吉克斯坦','塞舌尔','塞舌??','塞浦路斯','夏天?','夏日?', '多明尼加共和??,'多米尼加共和国','多米尼加共和??,'多米尼加国','多明尼加??,'穿梭?C','航天飞机','尼日利亚','尼日利??', '字符','字号','字库','字符集','存盘','安提瓜和巴布?','安提瓜和巴布达','宋元','洪都拉斯','寻址','寒假?','宽带','老??,'老挝', '打门','?比??','赞比亚','尼日??','尼日尔','山洞?','巴布??新畿???','巴布亚新几内亚','巴巴多斯','布基纳法索','布基?法索','布什','布殊','帕劳','例程', '平治之乱','平治之?y','几内亚比绍','????比?','彩带','彩排','彩楼','彩牌楼','?吞K','复苏','心?','快闪存储器','闪存','传感','习用','戏彩娱亲', '手电筒','手电','括号','拿破??,'拿破仑','?架','扫瞄仪','挂钩','?焘h','控件','台球','桌球','便携式','故事?','调制解调器','?制解?器','斯洛文尼??','斯洛文尼亚', '新纪元','新?元','日子?','春假?','春天?','春日?','?r??','芯片','暑假?','乍得','克林?','克林顿','格林??','格林纳达','凡高','森林?','榴?','榴莲','仿真', '毛里裘斯','毛里求斯','?C械人','机器人','字段','元音','永历','文莱','沙特阿拉伯','沙地阿拉伯','波斯尼??黑塞哥?那','波斯尼亚和黑塞哥维那','博茨瓦纳','博茨瓦?','侯赛因','侯?因', '深?Y?','光标','鼠标','算法','乌兹别克斯坦','词组','塞拉利昂','危地马拉','危地?拉','冈比亚','????','疑??,'疑凶','皮??秋','?旺?','卢旺达','真凶','真??,'硅片','硅谷', '硬盘','硬件','盘片','磁盘','磁道','福士','秋假?','秋天?','秋日?','程控','突尼斯','尾注','蹦极跳','绑紧跳','等于','短?','短信','新西?','新西兰','所罗门群岛','所??群?u', '索?里','索马里','结彩','佛得角','??','网络','互??','因特网','彩球','彩绸','彩线','彩船','彩衣' ,'缉凶','???,'?凶','意大利','老字号', '圣基茨和尼维斯','?吉斯?域斯','?文森特和格林?丁斯','圣文森特和格林纳丁斯','圣卢西亚','??西??','圣马力诺','??力?','肚?','肯尼亚','肯雅','任意球', '航天大学','苦?','毛里塔尼亚','毛里塔尼??','莫桑比克','万历','瓦努阿图','瓦努阿?D','也?','也门','着','科摩?','科摩罗','布隆迪','圭??那','圭亚那','火锅盖帽','苏里南', '行凶','行??,'行凶后','行?瘁?,'行凶後','流?与??','移动电话','行程控制','卫生','?生','埃塞俄比亚','埃塞俄比??','?勾外?','?面','分辨率','译码','出租车','权限','瑙鲁','瑙?', '变量','科特迪瓦','???','贝宁','伯利?','伯利兹','???,'买凶','?凶' ,'数据库','信息论','奔驰','平治','利比里亚','利比里??','?索托','莱索托','软驱','?件','软件','加载', '津巴布韦','津巴布?','词汇','加纳','加?','追凶','追??,'??','信道','逞凶?狠','逞?呆Y狠','逞凶斗狠','即食?','方便面','快速面','连字号','进制','入球','算子','?程控制','远程控制', '?丶{?D?','?院?','酰','巨商','钩','?','钩心斗角','?心?角','写保护','阿拉伯联合酋长国','阿拉伯?合酋???,'噪声','脱机','雪??','雪?蕻','雪铁龙','异步','声卡','缺省','颁布','???, '?域?','头球','粒入球','马里共和国','?里共和??,'马耳他','马尔代夫','???代夫','?事得','狄安娜','戴安娜','??','位图','商铺','商?','提交','店铺','店?','台湾','台湾','发布','?布'); return jtpyword; } function FTPYWord(){ var ftpyword = new Array('「','」','『','』','三?O?','三?O?','中文?','串列','串列加速器','乙太?','乳酪','二?O?','二?O?','互?邮?,'??塞拜然','人工智慧','介面','任意球?T','任意球?T', '伺服器','位元?','位元?','??先?序','元凶','元凶','光碟','光碟?C','克?埃西??','克?埃西??','全形','冬天?','冬日?','冷?','冷?', '凶器','凶器','凶徒','凶徒','凶手','凶手','凶案','凶案','凶??','凶??','凶??','凶??,'凶??,'凶??,'分散式','列印','列支敦斯登','剪?', '加彭','?R流排','?^域?','千里?托?哥','千里?托?哥','半形','卡?','卡?','印表?C','印表?C','厄利垂??','厄利垂??','厄瓜多','厄瓜多', '史瓦??m','史瓦??m','吉布地','吉布地','吉里巴斯','吐瓦?','吐瓦?','哈?克','哥斯大黎加','哥斯大黎加','?讨??','?讨??','?讨??','?讨??','嘴?', '土?炻?,'土豆','土豆?','土豆?','坦尚尼??','坦尚尼??','埠','塔吉克','塞席??','塞席??','塞普勒斯','夏天?','夏日?', '多明尼加','多明尼加','多明尼加','多米尼克','多米尼克','太空梭','太空梭','奈及利??','奈及利??', '字元','字型大小','字型?n','字符集','存?n','安地卡及巴布?','安地卡及巴布?', '宋元','宏都拉斯','定址','寒假?','???','寮??,'寮??, '射?','尚比??','尚比??','尼日','尼日','山洞?', '巴布???????','巴布???????','巴?多','布吉?法索','布吉?法索','布希','布希','帛琉','常式', '平治之?y','平治之?y','????比索','????比索','彩??,'彩排','彩??,'彩牌??,'?彤d','?彤d','心?','快?????','快?????','感?y','?T用','?蚓Z?视H', '手?筒','手?筒','括弧','拿破??','拿破??','捷豹','?呙槠?,'?煦^','?煦^','控制?','撞球','撞球','?y???,'故事?', '???C','???C','斯洛?尼??','斯洛?尼??', '新?元','新?元','日子?','春假?','春天?','春日?','?r??','晶元','暑假?','查德','柯林?','柯林?','格瑞那?','格瑞那?','梵谷','森林?','榴??','榴??','模?M', '模里西斯','模里西斯','?C器人','?C器人','?谖?,'母音','永??,'汶?','沙?醯匕⒗??,'沙?醯匕⒗??,'波士尼??赫塞哥??','波士尼??赫塞哥??','波札那','波札那','海珊','海珊', '深?Y?','游??,'滑鼠','演算法','?跗??e克','片?','?子山','瓜地?拉','瓜地?拉','甘比??','甘比??','疑凶','疑凶','皮??秋','?安?','?安?','真凶','真凶','矽片','矽谷', '硬碟','硬?','碟片','磁碟','磁?','福斯','秋假?','秋天?','秋日?','程式控制','突尼西??','章?附?','笨?跳','笨?跳','等於','??','??','?西?','?西?','索??群?u','索??群?u', '索?利??','索?利??','??','?德角','?路','?路','???路','???路','?球','??','??','?船','?衣','?凶','?凶','?凶','?大利','老字?', '?克里斯多福及尼?斯','?克里斯多福及尼?斯','?文森及格瑞那丁','?文森及格瑞那丁','?露西??','?露西??','??利?','??利?','肚?','肯??','肯??','自由球', '航天大?W','苦?','茅利塔尼??','茅利塔尼??','莫三比克','???,'?那杜','?那杜','??','??','著','葛摩','葛摩','蒲隆地','???那','???那','?火?','?利南', '行凶','行凶','行凶後','行凶後','行凶後','行?与??','行?与??','行程控制','?生','?生','衣索比??','衣索比??','?勾外?','?面','解析度','解?','?程?','?可??,'??','??', '???,'象牙海岸','?南','?南','?里斯','?里斯','?凶','?凶','?凶','?料??,'??理?','?士','?士','?比瑞??','?比瑞??','?索托','?索托','?碟?C','??','??','?入', '辛巴威','辛巴威','???,'迦?','迦?','追凶','追凶','??','通道','逞凶?狠','逞凶?狠','逞凶?狠','速食?','速食?','速食?','?字?','?位','?球','?算元','?程控制','?程控制', '那杜','?院?','醯','??','?','?','?心?角','?心?角','防??','阿拉伯?合大公??,'阿拉伯?合大公??,'??','??','雪??','雪?蕻','雪??','非同步','音效卡','??','?布','?布', '?域?','?槌','??球','?利共和??,'?利共和??,'???他','???地夫','???地夫','?自?','黛安娜','黛安娜','??','???D','展?','展?','送出','展?','展?','???,'???,'???,'???); return ftpyword; } function Traditionalized(cc){ var str='',ss=JTPYStr(),tt=FTPYStr(); var ssa = JTPYWord(),tta = FTPYWord(); 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); } for(var j=0;j<ssa.length;j++){ if(str.indexOf(ssa[j]) >= 0){ str = str.replace(ssa[j],tta[j]); }else{ str = str; } } return str; } function Simplized(cc){ var str='',ss=JTPYStr(),tt=FTPYStr(); var ssa = JTPYWord(),tta = FTPYWord(); 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); } for(var j=0;j<ssa.length;j++){ if(str.indexOf(tta[j]) >= 0){ str = str.replace(tta[j],ssa[j]); }else{ str = str; } } return str; } function setCookie(name, value) //cookies设置 { var argv = setCookie.arguments; var argc = setCookie.arguments.length; var expires = (argc > 2) ? argv[2] : null; if(expires!=null) { var LargeExpDate = new Date (); LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*3600*24)); } document.cookie = name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +LargeExpDate.toGMTString())); } function getCookie(Name) //cookies读取 { var search = Name + "=" if(document.cookie.length > 0) { offset = document.cookie.indexOf(search) if(offset != -1) { offset += search.length end = document.cookie.indexOf(";", offset) if(end == -1) end = document.cookie.length return unescape(document.cookie.substring(offset, end)) } else return "" } } var StranLink_Obj=document.getElementById("StranLink") if (StranLink_Obj) { var JF_cn="ft"+self.location.hostname.toString().replace(/\./g,"") var BodyIsFt=getCookie(JF_cn) if(BodyIsFt!="1")BodyIsFt=Default_isFT with(StranLink_Obj) { if(typeof(document.all)!="object") //非IE浏览器 { href="javascript:StranBody()" } else { href="#"; onclick= new Function("StranBody();return false") } title=StranText("点击以繁体中文方式浏览",1,1); innerHTML=StranText(innerHTML,1,1); } if(BodyIsFt=="1"){setTimeout("StranBody()",StranIt_Delay)} }
起初我以为是那个递归的问题引起的,但是测试发现就算是很多字符,它仍然能快速的进行替换。
经过排查,原来是JS和OBJECT标签会带来卡死的问题。繁简切换实现必然会跳过某些标签,看到这段,它的作用就是来判断替换哪些标签里的文字。
for(var i=0;i<obj.length;i++) { var OO=obj.item(i) if("||BR|HR|TEXTAREA|".indexOf("|"+OO.tagName+"|")>0||OO==StranLink_Obj)continue; if(OO.title!=""&&OO.title!=null)OO.title=StranText(OO.title); if(OO.alt!=""&&OO.alt!=null)OO.alt=StranText(OO.alt); if(OO.tagName=="INPUT"&&OO.value!=""&&OO.type!="text"&&OO.type!="hidden")OO.value=StranText(OO.value); if(OO.nodeType==3){OO.data=StranText(OO.data)} else StranBody(OO) }
加上这个就一切正常了:
if(OO.tagName=="OBJECT")continue; if(OO.tagName=="SCRIPT")continue;
网站繁简切换的JS遇到页面卡死的解决方法
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@