I’ve been diving deep into JavaScript lately and really enjoying it. But I’m starting to wonder if I’m going overboard with moving server-side functionality to the client side.
For example, instead of handling navigation highlighting on the server (I work with PHP), I’ve been using JavaScript to check the current page and style the active menu item:
var currentPath = document.location.href;
if (currentPath.includes('/contact')) {
document.getElementById('menu-contact').classList.add('active');
}
This approach feels clean and simple, but I worry about depending too much on JavaScript. What if users have it disabled? How do you all handle this balance between reliable server-side code that always works versus sleek client-side solutions that work most of the time?
I’m looking for advice on where to draw the line when deciding whether to implement features on the client or server side.
depends on your users tbh. building for general public? keep the important stuff server-side. but nav highlighting isn’t critical - most ppl won’t notice if js breaks. i’d handle forms/auth server-side and use js for the ui polish
Interesting dilemma! What kind of site are you building? Corporate client or personal project? Have you checked your analytics for users with JS disabled? It’s probably lower than you think. Why not try a hybrid approach - PHP handles the initial render, then JS takes over for smoother interactions?
You’ve raised an important point that many developers grapple with. While client-side solutions can enhance the user experience, they also introduce risks, especially if users have JavaScript disabled. It’s a good practice to prioritize server-side logic for fundamental functionalities, ensuring accessibility and reliability. By implementing essential features like navigation highlighting on the server with a brief PHP script, you can maintain full functionality for all users. Reserve client-side scripting for interactive elements that significantly improve user engagement. This way, you’re building a robust foundation that can benefit all visitors, regardless of their browser settings.