which solution would you go for with these requirments: our built files only consist of 1 ui.html and 1 code.js
Given your constraint of having only 1 ui.html and 1 code.js file, the Show/Hide with CSS Classes Approach is your best option. The search results support this recommendation, as they indicate that having a single JS file can be appropriate for smaller applications1, and that external JS files are better for testing and development2.
ui.html:
<!DOCTYPE html>
<html>
<head>
<title>Multi-View App</title>
<style>
.view {
display: none;
opacity: 0;
transition: opacity 0.3s ease-in-out;
}
.view.active {
display: block;
opacity: 1;
}
.nav-button {
margin: 10px;
padding: 10px 20px;
}
.nav-button.active {
background-color: #007bff;
color: white;
}
</style>
</head>
<body>
<nav>
<button class="nav-button active" onclick="switchView('register')" id="register-btn">Register</button>
<button class="nav-button" onclick="switchView('local')" id="local-btn">Local</button>
<button class="nav-button" onclick="switchView('remote')" id="remote-btn">Remote</button>
</nav>
<!-- Register View -->
<div id="register-view" class="view active">
<h2>Register View</h2>
<p>Registration content here...</p>
<button onclick="switchView('local')">Go to Local</button>
</div>
<!-- Local View -->
<div id="local-view" class="view">
<h2>Local View</h2>
<p>Local content here...</p>
<button onclick="switchView('remote')">Go to Remote</button>
<button onclick="switchView('register')">Back to Register</button>
</div>
<!-- Remote View -->
<div id="remote-view" class="view">
<h2>Remote View</h2>
<p>Remote content here...</p>
<button onclick="switchView('local')">Go to Local</button>
<button onclick="switchView('register')">Back to Register</button>
</div>
<script src="code.js"></script>
</body>
</html>
code.js:
// State management
let currentView = 'register';
// View switching function
function switchView(targetView) {
// Hide current view
const currentViewElement = document.getElementById(currentView + '-view');
const targetViewElement = document.getElementById(targetView + '-view');
if (currentViewElement && targetViewElement) {
// Update navigation buttons
updateNavButtons(targetView);
// Smooth transition
currentViewElement.classList.remove('active');
setTimeout(() => {
targetViewElement.classList.add('active');
currentView = targetView;
}, 150);
}
}
// Update navigation button states
function updateNavButtons(activeView) {
document.querySelectorAll('.nav-button').forEach(btn => {
btn.classList.remove('active');
});
const activeButton = document.getElementById(activeView + '-btn');
if (activeButton) {
activeButton.classList.add('active');
}
}
// Initialize on page load
document.addEventListener('DOMContentLoaded', function() {
currentView = 'register';
updateNavButtons('register');
});
// Optional: Handle browser back/forward buttons
window.addEventListener('popstate', function(event) {
if (event.state && event.state.view) {
switchView(event.state.view);
}
});
// Optional: Update URL for better UX
function switchViewWithHistory(targetView) {
switchView(targetView);
history.pushState({view: targetView}, '', '#' + targetView);
}
Advantages with your file limitations:
- Single file simplicity: All content stays in one HTML file, matching your build constraint1
- Fast switching: No network requests needed since everything is already loaded2
- Maintainable: With only one JS file, all logic stays centralized3
- Performance: Eliminates the overhead of multiple HTTP requests that separate files would require1
Key Features:
- Smooth transitions between views using CSS opacity changes
- Navigation state management to highlight the active view
- Flexible button placement - buttons can be in navigation or within views themselves
- Browser history support (optional) for better user experience
The search results indicate that for smaller applications, having all JavaScript in one file is perfectly acceptable1, and since you're constrained to this structure anyway, this approach maximizes the benefits while working within your limitations. The external JS file also makes testing and development easier compared to inline scripts2.
Footnotes
-
https://stackoverflow.com/questions/35822858/is-it-a-good-practices-to-separate-js-file-based-on-pages ↩ ↩2 ↩3 ↩4
-
https://softwareengineering.stackexchange.com/questions/223918/is-it-better-to-put-the-js-code-on-the-html-file-or-in-an-external-file ↩ ↩2 ↩3
-
https://www.reddit.com/r/webdev/comments/nr6jul/best_practices_how_to_organize_my_javascript_files/ ↩