3

Uservoice asks you to load async their widget on page load which the code looks like this:

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

And then they give you the option to run their widget on click as such:

javascript:UserVoice.showPopupWidget();

What if I dont want to load their widget once the page is loaded at all only when the user clicks on the link?

I would assume something like this:

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

   });

But it doesnt actually work? is there any other known way?

jQuerybeast
  • 14,130
  • 38
  • 118
  • 196

2 Answers2

5

Try this:

$('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
    );
});
AKX
  • 152,115
  • 15
  • 115
  • 172
jb10210
  • 1,158
  • 5
  • 15
  • This looks about right (with the edit I made). What it does is it uses jQuery to load and add the script to the page, and as soon as that script is loaded, the callback to show the popup widget is called. – AKX Feb 27 '12 at 12:28
  • And with the additional edit, the UserVoice script should never be loaded twice. – AKX Feb 27 '12 at 12:30
  • Any idea what the deal is with that random js filename? cyoJokWhM5SEW9I3h3oBFQ.js. I am reluctant to hard code that into my scripts as it looks like something that might change. Any advice? – Gwyn Howell Feb 09 '13 at 15:16
  • @GwynHowell I think the filename corresponds to the settings in UserVoice admin page: default tab selection, widget mode, etc. – Yeonho Jun 08 '13 at 17:32
  • I my project we are using user voice api for that I have to edit few things in my java script file( cyoJokWhM5SEW9I3h3oBFQ.js).How can I do this. – phani Jun 20 '18 at 11:53
0

Try this

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

see here for more details

lukeaus
  • 11,465
  • 7
  • 50
  • 60