XF 1.2 JavaScript funtion within xen:hook never gets invoked

localhost8080

Well-known member
Hello,

I need to have a simple javascript in my header to do some things.

On my template, I have the following code:

Code:
<xen:hook name="ad_header_above_logo">
<div class="logo" style="text-align:center;margin-top:5px;margin-bottom:1px;">
    <span>
<script type="text/javascript">
  <!--
function changeDuo() {
alert('1');
  window.setTimeout("changeDuo();",10000);
}
 //-->
</script>
    </span>
</div>
</xen:hook>

The function is never been called, even it should. not even the first alert-call.

is there an other way to call js-functions within hooks?

thank you.
 
You don't need to put code inside the hook tags unless you're modifying it in the hook PHP handler. In fact, you probably shouldn't do that.

However, in this case, your JS is just a function. Nothing is calling that function. (Your set timeout is within the function.)
 
For me, this works:

Code:
<script>
$(document).ready(function() {
    window.setTimeout("changeDuo();",1000);
});

function changeDuo() {
    alert('1');
    window.setTimeout("changeDuo();",1000);
}

</script>

Thanks to @Brogan I realise that the <!-- doesn't actually do anything to stop the javascript code. So that's fine.

Your main issue appears to be that the function changeDuo can't automatically execute on its own. You have window.setTimeout("changeDuo();",10000); which will execute the changeDuo function after 10 seconds, but because that execution happens in the function itself, it will never run.

So you first want to execute the changeDuo function using $(document).ready. Then it will execute every 10 seconds continuously.

EDIT: Yeah what @Mike said :)
 
Top Bottom