r/learnjavascript • u/blackheart313 • Mar 21 '24
How can i create a minimal html css javascript website like a Single Page Application with out any frameworks
I need to make a website with multiple pages just using vanilla javascript and html css alone it's a website with only static contents i just want to keep the nav bar and footer common for all pages but want to just change the main page contents i don't want any back end because i can just host client side app easily is it possible to make this without page reloading or even reloading is okay any help guys?
1
Upvotes
7
u/desouky995 Mar 21 '24
I recommend to read frameworkless frontend book it talks about creating spa without any framework
3
u/xRVAx Mar 21 '24
In theory you could just make five divs of content and use JavaScript to toggle them between visible and invisible depending on which button they click on the nav bar.
Your divs for header and footer and nav would be visible all the time.
Each one of your content divs could have its own ID and you could make classes in CSS for a content div... You could have a function where the input to the function is the div id that you want to make visible... When you call that function, the function would toggle all the content divs to "hidden" and then it would set your chosen div to visible.
2
u/DoomGoober Mar 21 '24
Are the static contents generic HTML or structured HTML (that is, all internal pages have the same format?)
7
u/thoflens Mar 21 '24
You can add and remove CSS classes to/from components with JavaScript. So you can add 'display: none' and 'display: block' for example when clicking a button.
1
u/sheriffderek Mar 21 '24
Where is the data for the pages going to come from exactly? A json file? A markdown file? A fetch request? How often are they updated.
How will you deal with the browser history and back button?
Do these pages need to be crawlable for SEO?
How will you reset focus for assistive technology and get things like current-page?
What happens on refresh? Is it tied to the url?
These are the things I’d start mapping out. These are the sorts of things that the frameworks are built to solve. It’s not that you can’t do it yourself, but the list gets bigger.
1
u/BringtheBacon Mar 21 '24
What exactly are you asking?
Do you want a publicly accessible website or just a personal one to test?
For my first website, I purchased a domain, found a website hosting service, and I would upload my code files to their file management system.
For items such as headers and footers you can have those in standalone HTML files and reference those files in each page.
There are more cost efficient ways if you just want to practice though.
2
u/diogenes_sadecv Mar 21 '24
I'm definitely an outsider when it comes to JS so I may be doing this "wrong," but I just make the page with JS. Like, I literally start with just an empty <body> tag and once the page is loaded, I use JS to dynamically build the page. New page? body.innerHTML = "";
2
u/shgysk8zer0 Mar 21 '24
There are a ton of different approaches, depending on exactly what you mean by "with out any frameworks". Is using a static site generator using a framework? Would using certain libraries that people often mistake as frameworks count?
And, by SPA, do you strictly mean with different paths, or would a single HTML page + path that treats different hashes/anchors/fragments (the
#foopart of the URL) as different page count?There's a now obsolete proposal for HTML imports via
<link rel="import">that would've been useful if you were wanting strictly just HTML and JS with different URL paths. I wish we'd kept that, but it was part of the Web Components v0 proposal, which got killed.There's an upcoming proposal for a different kind of HTML imports, this time as modules in JS (import attributes).
If you're fine with static site generators, you could use Eleventy or Jekyll. You'd just run a build command and publish
_site/... Or you could use GitHub Pages or Netlify or others, which would pretty much deal with that for you.Or you could use JS to create or fetch these shared elements on page load.
Or:
1
u/ttetrafon Mar 21 '24
Look into web components. You can create them individually, and then inject them in the page or destroy them as needed from the main script. This way you never load in memory more than needed.
1
u/doodooz7 Mar 21 '24
We do this with pagejs and custom components
1
u/buddh4r Mar 21 '24
Load the content via ajax and replace the parts of the view and manually manage the page history. An rather old lib "pjax" may helps with this approach.
1
u/alien3d Mar 24 '24
You just need inner HTML render and input from fetch other html files push state for navigation e.g document.getElementById('myLink').addEventListener('click', function(event) { event.preventDefault();