如何使用带有javascript函数的自定义URL动态创建"共享此按钮"?

bry*_*bam 9 html javascript social ajax sharethis

我使用http://sharethis.com/按钮为网站上的用户提供了一种分享内容的简单方法.

他们在这里有关于"自定义按钮"的信息:http://help.sharethis.com/customization/custom-buttons

看起来像指定URL,在示例中他们只有像这样的html:

<span class="st_sharethis" st_url="http://mycustomurl.com" st_title="Sharing Rocks!"
displayText="ShareThis"></span>
Run Code Online (Sandbox Code Playgroud)

但我需要能够在不重新加载页面的情况下动态创建按钮.

所以,在一个flash应用程序我正在构建我有一个"分享按钮",触发网页上的javascript功能,使"弹出"div淡入屏幕中心,我想显示共享按钮.根据用户点击的共享按钮,它需要具有动态创建的URL.

重要的是,如果他们关闭对话框,然后再次单击共享按钮,我希望代码将使用新的按钮覆盖旧代码,使用新的url/titles.

所以,我创建了这个我从flash调用的javascript函数,同时从flash传递url.我将脚本和所有内容放在我的"弹出"div中,希望按钮将在该div内部呈现.

function shareChannel(theurl)
{
//I cant seem to find an example of what to put here to load the buttons, and give them a custom URL and title

//finally display the popup after the buttons are made and setup.
displaypopup();
}
Run Code Online (Sandbox Code Playgroud)

有没有人可能发布一个例子,说明如何在不重新加载页面的情况下做到这一点?

编辑:@Cubed

<script type="text/javascript">
function shareChannel(chaan)
          {

          document.getElementById('spID1').setAttribute('st_url', 'http://mycustomurl?chan='+chaan);
          document.getElementById('spID1').setAttribute('st_title', 'Custom Title is ['+chann+'] it works!');

          stButtons.locateElements();

          centerPopupThree();
          loadPopupThree();
          }

</script>
Run Code Online (Sandbox Code Playgroud)

基本上没有任何反应.我讨厌使用javascript,因为我真的不知道如何获取错误消息或调试.当我使用flex时,我有一个很好的调试器/控制台来解决问题.所以,抱歉我无法提供更多信息.当我删除时,我注意到了

document.getElementById('spID1').setAttribute('st_url', 'http://mycustomurl?chan='+chaan);
              document.getElementById('spID1').setAttribute('st_title', 'Custom Title is ['+chann+'] it works!');
Run Code Online (Sandbox Code Playgroud)

弹出窗口出现.

但是如果我在函数中有上面的setAttribute代码,弹出窗口永远不会出现,所以我假设代码中的某些内容正在打破它.有什么建议?或者至少我可以使用chrome来告诉我错误是什么?我尝试了检查元素控制台,但当我触发该功能时,没有任何东西出现在那里.

此外,在我的代码的顶部,他们让我使用:

    <script type="text/javascript">var switchTo5x=true;</script>
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js">
</script><script type="text/javascript">stLight.options({publisher:'mypubidgoeshere'});</script>
Run Code Online (Sandbox Code Playgroud)

我应该使用 stLight.locateElements();而不是stButtons.locateElements();

Cub*_*Eye 15

我一直在使用

stButtons.locateElements();
Run Code Online (Sandbox Code Playgroud)

通过AJAX加载内容后生成任何新按钮的功能,我想它也适用于此.

这个问题可能也有一些帮助:

Sharethis按钮不适用于通过ajax加载的页面

改善方案:

制作你的<div>并确保所有的<span>s都有ID,否则你可以给他们上课,但你需要遍历它们.

function shareChannel(theurl, thetitle)
    {
    document.getElementById('spanID1').setAttribute('st_url', theurl);
    document.getElementById('spanID1').setAttribute('st_title', thetitle);

    stButtons.locateElements();

    //finally display the popup after the buttons are made and setup.
    displaypopup();
    }
Run Code Online (Sandbox Code Playgroud)