Run JavaScript in Template

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?
 
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.)
 
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.)
That is equal to $(function() {});.

Maybe I did something else wrong then... hmm.
 
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.
 
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.
Nope, fresh 1.2 install.

I am thinking it might be an error in my code that causes it to not work as intended.
 
Back
Top Bottom