解决新浪微博分享按钮 中文URL出错的问题

日期:2011-11-03    阅读:58   分类:Web前端


本站使用了新浪微博的分享按钮,但遇到了一个问题;由于本站的编码统一是GBK,并且有的页面URL里面包含有中文,那么由于新浪微博的分享代码用的是encodeURIComponent,是按UTF-8的方式编码的,那么这些中文URL传递过去之后就会遇到问题。

看代码:

function loadSinaShare() {
    var logoPanel = document.getElementById("logoPanel");
    if(!logoPanel)
    {
        return;
    }

    var _w = 90 , _h = 24;
    var param = {
        url:location.href,
        type:'2',
        count:'1',
        appkey:'',
        title:'',
        pic:'',
        ralateUid:'1508522870',
        rnd:new Date().valueOf()
     };

     var temp = [];
     for( var p in param ){
        temp.push(p + '=' + escape( param[p] || '' ) );
       //此处原来不是escape,原来为encodeURIComponent
     }

     var iframe = document.createElement("iframe");
     iframe.allowTransparency = true;
     iframe.frameBorder = 0;
     iframe.scrolling = "no";
     iframe.width = _w;
     iframe.height = _h;
     iframe.style.marginLeft = "4px";
     iframe.src = "http://hits.sinajs.cn/A1/weiboshare.html?"+ temp.join('&');

     logoPanel.appendChild(iframe);
}

扩展阅读:Javascript URL编码方法的比较

本页链接: http://www.scriptlover.com/static/1066-sina-url-按钮-微博

标签:

相关文章

网友评论

Leave a comment

 required

 required (Not published)

 required