jq的getScript函数不支持chaset?override掉!

其实我从来不用jq或者其他框架的,这两天偶然在一个小项目里面发现jq的一个小bug:getScript函数没有透传charset信息,如果试图在页面上加载一个跨编码的脚本的时候会导致编码错误。写了一个补丁函数覆盖掉原来的:

$.getScript= function(url, callback , charset){
    $.ajax({
        url: url,
        dataType: "script",
        success:callback,
        scriptCharset:charset
    })
}    

这几年代码写得很少,轻喷。这里是 demo代码 ,同时也到jq的github上提交了 一个issue

<! DOCTYPE html >
< HTML >
< HEAD >
< meta  charset ="utf-8"   />
< script  src ="http://cdn.jsdelivr.net/jquery/1.11.1/jquery.js" ></ script >

< SCRIPT  LANGUAGE ="JavaScript" >
<!--
function  log(s){
    $(
" body " )[ 0 ].innerHTML += " <p> " + s.replace( / \n / g, " <br> " ).replace( / \t / g, " &nbsp&nbsp&nbsp&nbsp " ) + " </p> " ;
}
function  testBIG5(result){
    log(
" BIG5 decode  " + (result ? " correctly " : " <font color=red>incorrectly</font> " ))
}
function  testGB(result){
    log(
" gb2312 decode  " + (result ? " correctly " : " <font color=red>incorrectly</font> " ))
}
function  testUTF8(result){
    log(
" utf-8 decode  " + (result ? " correctly " : " <font color=red>incorrectly</font> " ));
}
function  testJP(result){
    log(
" iso-2022-jp decode  " + (result ? " correctly " : " <font color=red>incorrectly</font> " ));
}
function  testKR(result){
    log(
" euc-kr decode  " + (result ? " correctly " : " <font color=red>incorrectly</font> " ));
}
$(document).ready(
function (){

    $.when(
        log(
" <i>old version of getScript:</i> " ),
        log($.getScript.toString()),
        $.getScript(
" http://stonelf.sinaapp.com/testGB.js " ),
        $.getScript(
" http://stonelf.sinaapp.com/testUTF8.js " ),
        $.getScript(
" http://stonelf.sinaapp.com/testBIG5.js " ),
        $.getScript(
" http://stonelf.sinaapp.com/testJP.js " ),
        $.getScript(
" http://stonelf.sinaapp.com/testKR.js " )
    ).then(
function (){

        $.getScript
= function (url, callback , charset){
            $.ajax({
                url: url,
                dataType: 
" script " ,
                success:callback,
                scriptCharset:charset
            })
        }    

        log(
" <hr><i>new versioni of getScript:</i> " );
        log($.getScript.toString()),
        $.getScript(
" http://stonelf.sinaapp.com/testGB.js " ,undefined, " gb2312 " );
        $.getScript(
" http://stonelf.sinaapp.com/testUTF8.js " ,undefined, " utf-8 " );
        $.getScript(
" http://stonelf.sinaapp.com/testBIG5.js " ,undefined, " big5 " );
        $.getScript(
" http://stonelf.sinaapp.com/testJP.js " ,undefined, " iso-2022-jp " );
        $.getScript(
" http://stonelf.sinaapp.com/testKR.js " ,undefined, " euc-kr " );

    })
})
// -->
</ SCRIPT >
</ HEAD >
< BODY >
</ BODY >
</ HTML >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值