Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Using Javascript to easily place html on multiple pages?

Hi there, lovely peoples of Treehouse.

I have a problem, I have about 450 html pages that need a social media link section, and was wondering if it was possible to use Javascript to easily import that bit of HTML code into the pages, is this possible? Or would I have to manually place them?

Thank you!

3 Answers

Trust me, I've been in your position before.

After taking the PHP course on Treehouse, I found the following method to be useful.

After converting all of my pages to .php instead of .html, I used the "include();" function to inclue the header and footer from header.php and footer.php files.

That way any time you need to edit something simple, like adding a social media link, you only have to do it once.

I know that sounds like a lot of work to get set up, but it has saved me many hours of painstaking work to change something small.

I recommend going through the PHP track on Treehouse to learn more.

Hi Sylinic,

If there is an empty container available on every page, and each of those pages links to your JS file you can use document.getElementById('IDofDiv').innerHTML = '<Your HTML here>';

That would sort your issue, but there are the dependencies mentioned above.

Hope it works out!

Louis and Adam, thanks so much for the prompt and detailed responses, you guys really pointed me in the right direction to learn more about this.

Much gratitude!

You're very welcome. :)