2012-05-04 59 views
0

任何想法爲什麼把谷歌加代碼這種方式不會工作在IE8或任何其他版本我猜?雖然在Firefox中工作得很好。在IE8中的JavaScript奇怪,document.write問題

現場演示:http://jsfiddle.net/9zqsZ/

<script> 

var socialString = '<g:plusone size="tall"></g:plusone>'; 
document.write(socialString); 

    //google plus share button 
    (function() { 
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; 
    po.src = 'https://apis.google.com/js/plusone.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); 
    })(); 

</script> 

我只是試圖把它放在外部文件中。奇怪的是,它只是不適用於document.write,並且如果直接放在html中,它就會工作。如何在這種情況下實現它?

回答

1

你可以這樣做:

<script> 
    if (navigator.appVersion.indexOf("MSIE") != -1) { 
     var socialString = '<g:plusone size="tall"></g:plusone>'; 
     var newEle = document.createElement(socialString); 
     document.body.appendChild(newEle); 
    } 
    else { 
     var socialString = '<g:plusone size="tall"></g:plusone>'; 
     document.write(socialString); 
    } 

    //google plus share button 
    (function() { 
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; 
    po.src = 'https://apis.google.com/js/plusone.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); 
    })(); 
</script> 
+0

我的控制檯返回一個錯誤把你的代碼「字符串包含無效字符」 – devjs11

+0

這就是奇怪,它在IE8工作對我來說,可能會有一些額外的字符得到了增加,同時你複製了代碼,請檢查一次 –

+0

好了,現在應該可以正常工作,查看編輯答案 –