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

ilovecode
ilovecode
8,071 Points

How to solve JS errors appearing on different pages due to one JS file

I hope I can explain this in a way that is understandable. I have googled everywhere and I am really lost.

I have a web application on my localhost, with 3 php web pages. The 3 pages use a header and footer includes in each of these pages. Let's call the pages home.php, about.php and contact.php.

I include all my .js files in the footer.php file. But now I have one .js file (custom.js) that I inserted in the footer.php includes file.

Now I am getting errors on the home.php, etc pages if there is js code with DOM selectors not on that page. E.g document.querySelector('.home-body') gives error on about.php and contact.php. Does this mean I have to have a separate js file for each page and insert them inside those files?

I hope this is clear.

Thanks

I would add that this is a very inefficient way of creating a web application. I've worked with plain PHP, but this is really not a good solution for any site or app unless it's just a single page with a simple back-end. If you like working with PHP I would encourage you to invest the overhead to learn the Slim framework. From there once you understand MVC fundamentals you could learn Laravel easier.

1 Answer

You will get an undefined message if you include a JavaScript file in a PHP file that does not contain the relevant HTML. If you were to use a templating language with a PHP framework like Slim or Laravel then you could solve this problem by dynamically adding in the correct script. Since it sounds like you're using plain PHP and not using an MVC framework, you will just have to hard code in the correct script for each page.

ilovecode
ilovecode
8,071 Points

Does it also mean that I must then have a different .js file for each page? Currently I am just learning .js without using a framework because I plan to add backend functionality only later on as I learn. I want to learn all the raw concepts first before doing frameworks

Currently I would say yes. If you write one single js file for all your pages, then errors will spring up.

I would encourage learning one language at a time. If you're trying to build a website with PHP and JavaScript at the same time you might be biting off more than you can chew. I would recommend working through the Introducing JavaScript track and a first starting point, and then take a PHP track.

ilovecode
ilovecode
8,071 Points

Thanks, I have learnt some PHP already, so I am doing the Full Stack Javascript Track now.