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

Run JavaScript in Template

tyteen4a03

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

Chris D

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

tyteen4a03

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

Chris D

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

tyteen4a03

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