1. 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

Discussion in 'XenForo Questions and Support' started by localhost8080, Jul 23, 2013.

  1. localhost8080

    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.
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    Isn't it commented out?

    The Javascript code begins with:

    <!--

    and ends with

    // -->

    I think that will be disabling the code.
     
  3. Mike

    Mike XenForo Developer Staff Member

    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 likes this.
  4. Chris D

    Chris D XenForo Developer Staff Member

    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 :)
     
  5. localhost8080

    localhost8080 Well-Known Member

    I got a rotating image in the hook, which changes every 10 seconds.. so I put it in the hook. but how can I fire a body onload event?
     
  6. xf_phantom

    xf_phantom Well-Known Member

    Code:
    $(document).ready(function() {
      changeDuo();
    });
    
     
    localhost8080 likes this.
  7. localhost8080

    localhost8080 Well-Known Member

    thanks. it's working fine now.
     

Share This Page