Skip to content

Instantly share code, notes, and snippets.

@kangax
Created April 28, 2010 01:50
Show Gist options
  • Save kangax/381634 to your computer and use it in GitHub Desktop.
Save kangax/381634 to your computer and use it in GitHub Desktop.
var uservoiceOptions = {
/* ... */
};
function _loadUserVoice() {
var s = document.createElement('script');
s.setAttribute('type', 'text/javascript');
s.setAttribute('src', ("https:" == document.location.protocol ? "https://" : "http://") + "cdn.uservoice.com/javascripts/widgets/tab.js");
document.getElementsByTagName('head')[0].appendChild(s);
}
_loadSuper = window.onload;
window.onload = (typeof window.onload != 'function') ? _loadUserVoice : function() { _loadSuper(); _loadUserVoice(); };
@kangax
Copy link
Author

kangax commented Apr 28, 2010

Alternative version:

window.onload = (function(original){

  function _loadUserVoice() {
    var uv = document.createElement('script');
    uv.async = true;
    uv.src = ("https:" == document.location.protocol ? "https://" : "http://") + "cdn.uservoice.com/javascripts/widgets/tab.js";
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(uv, s);
  }

  if (typeof original == 'function') {
    return function() {
      original(); 
      _loadUserVoice();
    };
  }
  return _loadUserVoice;

})(window.onload);

@MarkoZabcic
Copy link

What if there is no script tag on the page?

@kangax
Copy link
Author

kangax commented Apr 29, 2010

There should always be a script element in a document... at least the one from within which this snippet is running. The snippet could of course be in event handler attribute, but that seems very unlikely.

@fearphage
Copy link

Is there a reason you are using window.onload instead of event listeners?

Also:
return (typeof original == 'function'
? function() {
original();
_loadUserVoice();
}
: _loadUserVoice
);

Less repetition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment