繁体   English   中英

如何使用带有 javascript 功能的自定义 URL 动态创建“共享此按钮”?

[英]How to dynamically create "Share This buttons" with a custom URLs with a javascript function?

我使用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 中,希望按钮将呈现在该 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 按钮在通过 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();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM