Javascript 通过JS进行交互式共享而不是渲染
有人能解释一下为什么下面的代码不起作用吗?我已经从谷歌复制了代码,我需要“onshare”功能,我不相信这是通过标准HTML提供的。没有JS错误-也没有任何错误:Javascript 通过JS进行交互式共享而不是渲染,javascript,google-plus,Javascript,Google Plus,有人能解释一下为什么下面的代码不起作用吗?我已经从谷歌复制了代码,我需要“onshare”功能,我不相信这是通过标准HTML提供的。没有JS错误-也没有任何错误: <!DOCTYPE html> <html> <head> <title>Title of the document</title>
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<script src="https://apis.google.com/js/platform.js" async defer></script>
</head>
<body>
<h1 style="font-family:arial;font-size:15px;">google_test</h1> <div id="sharePost"></div>
<script>
var options = {
contenturl: 'https://plus.google.com/pages/',
contentdeeplinkid: '/pages',
clientid: 'YOUR ID HERE',
cookiepolicy: 'single_host_origin',
prefilltext: 'Create your Google+ Page too!',
calltoactionlabel: 'CREATE',
calltoactionurl: 'http://plus.google.com/pages/create',
calltoactiondeeplinkid: '/pages/create',
onshare: function(response){
alert(response);
}
};
gapi.interactivepost.render('sharePost', options);
</script>
</body>
</html>
文件标题
谷歌测试
变量选项={
contenturl:'https://plus.google.com/pages/',
contentdeeplinkid:“/pages”,
clientid:'您的ID在这里',
cookiepolicy:“单主机源”,
prefilltext:'也创建您的Google+页面!',
calltoactionlabel:'创建',
calltoactionurl:'http://plus.google.com/pages/create',
calltoactiondeeplinkid:“/pages/create”,
onshare:功能(响应){
警报(响应);
}
};
gapi.interactivepost.render('sharePost',选项);
经过一些调查(5/1/15),我发现在响应(firebug中的网络面板)中,我在一个隐藏的文本框中得到了一个错误。这是因为我的源url错误,但现在我得到以下信息:
<input type="hidden" id="error" value="true" />
<input type="hidden" id="response-form-encoded" value="state=887621045%7C0.2365188186&error=immediate_failed&num_sessions=1&session_state=MY_SESSION_ID" />
你知道这意味着什么吗?你用异步延迟加载谷歌api。因此,当您创建共享链接时,它还没有准备好
您应该仅在页面加载后呈现共享链接(使用domready事件),或者删除“async defer”属性 答案很简单-谢谢你帮我看。在上面的例子中
<div id="sharePost"></div>
只需要以下内容:
<div id="sharePost"><button>hello world</button></div>
你好,世界
事实上,你甚至不需要按钮!!我不知道为什么文档中没有这个,所以我报告文档不是很好。此外,您应该注意callbackurl和calltoactionurl都需要相同的协议——它们的示例也不同。删除了异步延迟,我不担心。