2011-07-02 25 views
2

要在您的网站上添加Google plusone按钮,将插入以下脚本标记(用于显式加载)。更改IE中脚本标记的innerHTML以明确加载谷歌plusone按钮

<script type="text/javascript" src="https://apis.google.com/js/plusone.js"> 
    {"parsetags": "explicit"} 
</script> 

它在HTML中看起来很直截了当。但是我不想使用JS文件插入脚本。所以我用下面的代码:

var e = document.createElement('script'); 
e.src = "https://apis.google.com/js/plusone.js"; 
e.id = "googplusonescript"; 
e.innerHTML = '{"parsetags": "explicit"}'; 
document.getElementsByTagName("head")[0].appendChild(e); 

它可以在除了IE以外的浏览器相当真棒,因为IE不允许我们写脚本标记的innerHTML。任何工作都围绕着任何人? (我的jQuery在网页中插入这样可以jQuery的使用太多。)

回答

5

在同样的问题来了。 在IE下你应该使用 脚本。 text ='{“parsetags”:“explicit”}'; 代替script.innerHTML

+0

谢谢@Evresh – Nili

1

尝试创建一个textNode并将其附加到脚本标记:

var myText = document.createTextNode('{"parsetags": "explicit"}'); 
myScriptTag.appendChild(myText); 
+0

不能在IE工作。 :( – Sharad

-1
var ispoloaded; 

function showpo() { 
var pohd = document.getElementsByTagName('head')[0]; 
var poscr = document.createElement('script'); 
poscr.type ='text/javascript'; 
poscr.async = true; 
poscr.text ='{"parsetags": "explicit"}'; //works on IE too 
poscr.src = "http://apis.google.com/js/plusone.js"; 
pohd.appendChild(poscr); 


ispoloaded = setInterval(function() { 


//check if plusone.js is loaded 
if(typeof gapi == 'object') { 
//break the checking interval if loaded 
clearInterval(ispoloaded); 
//render the button, if passed id does not exists it renders all plus one buttons on page 
gapi.plusone.go("idthatexists"); 
} 

}, 100); //checks every 0.1 second 



} 
0

尝试以下操作:

window['___gcfg'] = { parsetags: 'explicit' };