【问题标题】:Load uservoice API on Click点击时加载用户语音 API
【发布时间】:2012-02-27 11:58:44
【问题描述】:

Uservoice 要求您在页面加载时异步加载他们的小部件,代码如下所示:

var uvOptions = {};
          (function() {
            var uv = document.createElement('script'); uv.type = 'text/javascript'; uv.async = true;
            uv.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'widget.uservoice.com/loremipsum.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(uv, s);
          })();

然后他们为您提供了在点击时运行他们的小部件的选项:

javascript:UserVoice.showPopupWidget();

如果我不想在页面加载后仅在用户单击链接时加载他们的小部件怎么办?

我会假设是这样的:

 $('selector').click(function () {

         var uvOptions = {};
          (function() {
            var uv = document.createElement('script'); uv.type = 'text/javascript'; uv.async = true;
            uv.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'widget.uservoice.com/cyoJokWhM5SEW9I3h3oBFQ.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(uv, s);
          })();
       javascript:UserVoice.showPopupWidget();

   });

但它实际上不起作用?还有其他已知的方法吗?

【问题讨论】:

    标签: javascript jquery asynchronous widget uservoice


    【解决方案1】:

    试试这个:

    $('selector').click(function () {
        var uvOptions = {};
        var showWidget = function() { window.UserVoice.showPopupWidget(); };
        if(window.UserVoice) return showWidget(); // Loaded already
        $.getScript(
           ('https:' == document.location.protocol ? 'https://' : 'http://') +
           'widget.uservoice.com/cyoJokWhM5SEW9I3h3oBFQ.js',
           showWidget
        );
    });
    

    【讨论】:

    • 这看起来很正确(我所做的编辑)。它的作用是使用 jQuery 加载脚本并将其添加到页面,一旦加载该脚本,就会调用显示弹出窗口小部件的回调。
    • 加上额外的编辑,UserVoice 脚本永远不会被加载两次。
    • 知道随机的 js 文件名是怎么回事吗? cyoJokWhM5SEW9I3h3oBFQ.js。我不愿意将它硬编码到我的脚本中,因为它看起来可能会改变。有什么建议吗?
    • @GwynHowell 我认为文件名对应于 UserVoice 管理页面中的设置:默认选项卡选择、小部件模式等。
    • 我的项目使用的是用户语音 api,因此我必须在我的 java 脚本文件 (cyoJokWhM5SEW9I3h3oBFQ.js) 中编辑一些内容。我该怎么做。
    【解决方案2】:

    试试这个

    <a href="http://back-up-url-goes-here.com" data-uv-trigger>Contact us</a>
    

    here for more details

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-08
      • 2013-10-26
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多