Separating JS & HTML in WordPress

I recently read the discussion https://stackoverflow.com/questions/62617/whats-the-best-way-to-separate-php-code-and-html

I am facing a similar dilemma.

Read More

Currently working on a WordPress website which has a LOT of sliders, animated dropdowns, forms and other components. Each of component is used multiple times throughout the website, so I’ve created a PHP file for each of these components and I use include to insert them wherever they are required.

Now the file I am including contains HTML & JS {for initializing control}

And finally when I looked at the generated html page in browser, it was chaotic! HTML & JS mixed everywhere!

I wanted to know if its better to include everything in one big JS file and include it on top of every page or its more efficient to have small JS blocks in the section?

Related posts

Leave a Reply

2 comments

  1. In my opinion, if the HTML/JS is valid, there’s nothing wrong with it being a bit messy – the end user will never see it anyway. As for what’s best from a design point of view, it really depends on the situation.

    Personally, I think it’s alright to have a big JS file that’s included in every page, but some people prefer to output the code in small blocks where it’s needed. Ultimately, it really depends on what the program is and what works best for you.

  2. If the JS code is targeted to just a particular section or page i think it is better to include it in page itself instead of a separate file but if it is meant to be used in multiple places then use a separate file as it will also be easier to maintain it that way.