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

Run JavaScript in Template

Discussion in 'XenForo Development Discussions' started by tyteen4a03, Mar 20, 2014.

  1. tyteen4a03

    tyteen4a03 Well-Known Member

    I need to run a bit of JavaScript in a template - it is dynamically generated with the template. How can I hook it up so it runs properly? Right now it seems to fire before xenforo.js is loaded and I think it causes my JavaScript to not fire correctly.

    Should I wrap my code in $(function() {}) or XenForo.register?
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    You probably want to wrap it in:
    Code:
    $.document.ready(function()
    {
        // Your JS here.
    });
    That should defer running until the page has finished loading (by that point xenforo.js should have finished etc.)
     
  3. tyteen4a03

    tyteen4a03 Well-Known Member

    That is equal to $(function() {});.

    Maybe I did something else wrong then... hmm.
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    Yes, you're right. Sorry, I didn't realise you'd mentioned that in your post :unsure:
     
  5. Chris D

    Chris D XenForo Developer Staff Member

    Are you running any add-ons - specifically the DeferJS add-on that recently went into the Resource Manager?

    That would definitely cause your code to execute before xenforo.js because it places all the JS in the footer.
     
  6. tyteen4a03

    tyteen4a03 Well-Known Member

    Nope, fresh 1.2 install.

    I am thinking it might be an error in my code that causes it to not work as intended.
     
    Chris D likes this.

Share This Page