• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 1.2 JavaScript funtion within xen:hook never gets invoked

localhost8080

Well-known member
#1
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.
 

Chris D

XenForo developer
Staff member
#2
Isn't it commented out?

The Javascript code begins with:

<!--

and ends with

// -->

I think that will be disabling the code.
 

Mike

XenForo developer
Staff member
#3
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.)
 

Chris D

XenForo developer
Staff member
#4
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 :)