For some reason the spinner object is working only if it is defined inside the startSpin() function.
This is the non-working code:
<script type="text/javascript" src="resources/js/spin.js"></script>
<script type="text/javascript">
var opts = {
lines: 18, // The number of lines to draw
length: 40, // The length of each line
top: 'auto', // Top position relative to parent in px
left: 'auto' // Left position relative to parent in px
};
// -- not support ?
var target = document.getElementById('spin');
var spinner = new Spinner(opts).spin(target);
// -- ???
function startSpin()
{
spinner.start();
}
function stopSpin()
{
spinner.stop();
}
function showStatus() {
startSpin();
statusDialog.show();
}
function hideStatus() {
stopSpin();
statusDialog.hide();
}
</script>
<h:form id="testfm">
<p:commandButton id="start" type="submit"
ajax="false"
value="test"
actionListener="#{bean.test}"
onclick="PrimeFaces.monitorDownload(showStatus, hideStatus)"/>
<p:dialog modal="true"
widgetVar="statusDialog"
showHeader="false"
draggable="false"
closable="false"
resizable="false">
<div id="spin" class="spinner"/>
</p:dialog>
</h:form>
the spinner is working only when it is defined inside the spinStart function
I try to play with the script position but still get the same message
any idea why?
Thanks
You you run
document.getElementById('spin')outside of that function, the element withid=spinhas not been created yet, so you are giving a null value to the spinner. If you create it insidestartSpin, it is in response to a click event from the user, so the DOM has likely been built at that point and the element exists. Here is a workaround:You could also leave the code as it is and put it at the end of your document, just before the
</body>tag.