In an HTML page, how can I know when a externally loaded Javascript object is defined and ready for methods to be called on it?
Problem specifics:
I'm using the uservoice feedback web application including their Javascript widget and their SSO (Single-SignOn) option.
Everything works fine. There is a prominent Feedback tab stuck on the left of the window. And I can use the following anchor for additional links to open their popup feedback widget:
<a href="#" onclick="UserVoice.Popin.show(uservoiceOptions); return false;">feedback</a>
But... I'm having trouble trying to support a extra workflow - on one particular page (e.g. the feedback page) I want the uservoice feedback form to automatically popup when the user goes to the page without being initiated via a click event.
I tried putting the following at the bottom of the page:
<script type="text/javascript">
function _autoPopupUserVoice() {
UserVoice.Popin.show(uservoiceOptions);
}
_loadSuper = window.onload;
window.onload = (typeof window.onload != 'function') ? _autoPopupUserVoice : function() { _loadSuper(); _autoPopupUserVoice(); };
</script>
But Firebug shows a error:
"UserVoice is not defined".
So I guess the uservoice Javascript hasn't been executed by the time _autoPopupUserVoice()
is called.
I've tried a few other things but haven't managed to get it working. How can know when the Uservoice
Javascript object is loaded and ready for methods to be called on it?
For reference the Uservoice object is loaded via the following Javascript at the end of the page:
<script type="text/javascript">
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(); };