jQu*_*ast 3 javascript jquery asynchronous widget uservoice
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);
})();
Run Code Online (Sandbox Code Playgroud)
然后他们为您提供了在点击时运行其小部件的选项:
javascript:UserVoice.showPopupWidget();
Run Code Online (Sandbox Code Playgroud)
如果我只是在用户点击链接时加载页面后才想加载小部件怎么办?
我会假设这样的事情:
$('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();
});
Run Code Online (Sandbox Code Playgroud)
但它实际上没有用?有没有其他已知的方式?
试试这个:
$('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
);
});
Run Code Online (Sandbox Code Playgroud)