2012-02-27 70 views
3

UserVoice的要求您異步他們在頁面加載插件其代碼如下加載:負載UserVoice的API

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); 
      })(); 

然後他們給你來運行其上點擊widget作爲這樣的選項:

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(); 

    }); 

但它不實際工作?有沒有其他已知的方法?

回答

5

試試這個:

$('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 
    ); 
}); 
+0

這看起來約權(與我做了修改)。它使用jQuery來加載腳本並將其添加到頁面中,並且一旦該腳本被加載,就會調用顯示彈出窗口小部件的回調函數。 – AKX 2012-02-27 12:28:22

+0

並且通過額外的編輯,UserVoice腳本不應該被加載兩次。 – AKX 2012-02-27 12:30:20

+0

任何想法是什麼交易與隨機js文件名? cyoJokWhM5SEW9I3h3oBFQ.js。我不願意將它硬編碼到我的腳本中,因爲它看起來可能會發生變化。有什麼建議? – 2013-02-09 15:16:08

0

試試這個

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

看到here for more details