http://open.weibo.com/widget/followbutton.php
掩饰了一个htm如何实现,编写了一个简单的htm测试代码,的确也没有问题.现在问题是,index.htm需要对多浏览器的语言环境进行判断,转到不同的页面:cn.htm或en.htm.根据上面的参考代码, 修改了一个index.htm, 代码如下:
<html xmlns:wb=“http://open.weibo.com/wb”>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="Shortcut Icon" href="favicon.ico">
<script src="http://tjs.sjs.sinajs.cn/open/api/js/wb.js" type="text/javascript" charset="utf-8"></script>
<title>Welcome to</title><style type="text/css">
.style1 {
text-align: center;
}
.style2 {
text-align: right;
}
.style3 {
text-align: left;
}
</style>
</head><body><script>  
function createXMLHttpRequest(){ 
    if(window.XMLHttpRequest){ 
        XMLHttpR = new XMLHttpRequest(); 
    }else if(window.ActiveXObject){ 
        try{ 
            XMLHttpR = new ActiveXObject("Msxml2.XMLHTTP"); 
        }catch(e){ 
            try{ 
                XMLHttpR = new ActiveXObject("Microsoft.XMLHTTP"); 
            }catch(e){ 
            } 
        } 
    } 
} function sendRequest(url){ 
    createXMLHttpRequest(); 
    XMLHttpR.open("GET",url,true); 
    XMLHttpR.setRequestHeader("Content-Type","text/html;charset=utf-8"); 
    XMLHttpR.onreadystatechange = processResponse; 
    XMLHttpR.send(null); 
} function processResponse(){ 
    if(XMLHttpR.readyState ==4 && XMLHttpR.status == 200){ 
        //document.write(XMLHttpR.responseText); 
        document.body.innerHTML = XMLHttpR.responseText;
    } 

</script><script>
var type=navigator.appName  
if (type=="Netscape")  
var lang = navigator.language  
else 
var lang = navigator.userLanguage 
if (lang.substr(0,5) == "zh-cn")
{
sendRequest("http://www.xxx.com/cn.htm")
}
else
{
sendRequest("http://www.xxxx.com/en.htm")
}
</script>
 
</body>
</html>cn.htm代码:
<div class="style2">
<wb:follow-button uid="2991975565" type="red_3" width="67" height="24" ></wb:follow-button></div>
en.htm代码:
<div class="style3">
<wb:follow-button uid="2991975565" type="red_3" width="67" height="24" ></wb:follow-button></div>
奇怪的,index.htm第一次并不会现实那个微博的加关注按钮, 而是要点击刷新按钮后, 才能现实显示.请教, 如果index.htm一加载就实现呢?

解决方案 »

  1.   

    由于ajax是异步的,有可能新浪分享执行DOM操作生成按钮的时候ajax都还没返回更新dom,所以找不到<wb:share-button ></wb:share-button>这种标签导致无法生成 XMLHttpR.open("GET",url,/*true*/false); //改同步,不过网速慢浏览器会假死。。异步的话需要动态加载分享的js
        function loadShared() {//动态加载分享的js
            var scr = document.createElement('script');
            scr.type = 'text/javascript';
            scr.charset = 'utf-8';
            scr.src = 'http://tjs.sjs.sinajs.cn/open/api/js/wb.js?_dc=' + new Date().getTime();
            document.getElementsByTagName('head')[0].appendChild(scr);
        }    function processResponse() {
            if (XMLHttpR.readyState == 4 && (XMLHttpR.status == 200 || XMLHttpR.status==0)) {
                //document.write(XMLHttpR.responseText); 
                document.body.innerHTML = XMLHttpR.responseText;
                loadShared();//ajax返回数据后再加载js文件
            }
        } 
      

  2.   


    感谢回复,那twitter的follow按钮代码应该也是如此?
    这是https://dev.twitter.com/docs/follow-button 提供的引用代码:1.<a href="https://twitter.com/twitterapi" class="twitter-follow-button" data-show-count="false" data-lang="en">Follow @twitterapi</a>2.<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>我把
    1.的内容放入en.htm
    2.的内容放入index.htm. 问题和上面的一样,按钮没有出现, 只有一个Follow @twitterapi的链接出现.应该如何修改?