如何使用JavaScript功能自定义网址动态创建“共享此按钮”?

im使用 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>

但是,我需要能够动态创建按钮,而无需重新加载页面.

所以,在一个Flash应用程序,我正在建立我有一个分享按钮”,触发一个JavaScript功能的网页,使一个“弹出”的div渐变到屏幕的中心,我想显示共享按钮.根据某人点击的分享按钮,需要一个动态创建的URL.

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

所以,我创建了这个从Flash中调用的JavaScript函数,同时从flash传递url.我将脚本和所有内容都放在我的“弹出”区域内,希望这些按钮将在该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();
}

有没有人可能会发表一个例子,我可以如何做到这一点,而不用重新加载页面

编辑:
@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>

基本没什么发生我讨厌使用javascript,因为我真的不知道如何获取错误消息或调试.当我使用flex,我有一个伟大的调试器/控制台来解决.所以,抱歉我提供更多的信息.我确实注意到,当我删除只是

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

弹出窗口出现.

但是如果我在函数中有上面的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>

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

解决方法

我一直在使用
stButtons.locateElements();

功能在通过AJAX加载内容生成任何新的按钮,我想象它也将工作.

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

Sharethis button does not work on pages loaded via ajax

更好的解决方案:

使您的< div>并确保所有的< span>都有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();
    }

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...