腾讯微博发布 JS 错误分享文字间有空格时失败
以下是腾讯官方的JS 分享案例:<!-- 腾讯微博发布 -->
<img src="http://v.t.qq.com/share/images/s/weiboicon16.png" align="absmiddle" border="0" alt="转播到腾讯微博" /> <a href="javascript:void(0)" onclick="postToWb();return false;" style="height:16px;font-size:12px;line-height:16px;">转播到腾讯微博</a>
<script type="text/javascript">
function postToWb(){
var _url = encodeURIComponent(document.location);
var _assname = encodeURI("");//你注册的帐号,不是昵称
var _appkey = encodeURI("801005914");//你从腾讯获得的appkey
var _pic = encodeURI('');//(例如:var _pic='图片url1|图片url2|图片url3....)
var _t = '';//标题和描述信息
var metainfo = document.getElementsByTagName("meta");
for(var metai = 0;metai < metainfo.length;metai++){
if((new RegExp('description','gi')).test(metainfo[metai].getAttribute("name"))){
_t = metainfo[metai].attributes["content"].value;
}
}
_t = document.title+_t;//请在这里添加你自定义的分享内容
if(_t.length > 120){
_t= _t.substr(0,117)+'...';
}
_t = encodeURI(_t);
var _u = 'http://share.v.t.qq.com/index.php?c=share&a=index&url='+_url+'&appkey='+_appkey+'&pic='+_pic+'&assname='+_assname+'&title='+_t;
window.open( _u,'', 'width=700, height=680, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no' );
}
</script>
在使用中发现,如果分享的内容中有多个空格就会出现,无法分享,分享的内容丢失的现象。
解决方法:
_t = encodeURI(_t);
改为 encodeURIComponent(_t);
腾讯做事马马虎虎,不注意细节,造成用户体验很差。。。。