DEV Community

Cover image for Simulate single page app using CSS selector (:target)
mohamed farid
mohamed farid

Posted on • Edited on

15 6

Simulate single page app using CSS selector (:target)

hack the idea of SPA using :target css selector (it is a funny idea more than for actual use ) preview

why target selector, because it is the only selector that can understand the URL.

the target selector refers to the element with the id matches
the URL hash

the idea is to give every page id and hide all pages except the current target page

and you can still navigate between pages without javascript
or refresh the page as normal

to hide all pages except the target page

 .page:not(:target) {
   display: none;
 }

Enter fullscreen mode Exit fullscreen mode

put nav to be fixed in all pages

<nav>
  <a href="#home">Home</a>
  <a href="#about">About</a>
  <a href="#contact">Contact</a>
  <a href="#posts">Posts</a>
</nav>
Enter fullscreen mode Exit fullscreen mode

create pages that will be affected

<div class="page home" id="home">Home</div>
<div class="page about" id="about">about</div>
<div class="page posts" id="home">posts</div>
Enter fullscreen mode Exit fullscreen mode

if the URL does not have hash push to the home page

if (window.location.hash === "") {
  window.location.hash = "#home";
}
Enter fullscreen mode Exit fullscreen mode

to listen to the page change

window.addEventListener("hashchange", (e) => {
});
Enter fullscreen mode Exit fullscreen mode

as example, if you went to get posts

window.addEventListener("hashchange", (e) => {
  if (location.hash === "#posts") {
    fetch("https://sitetogetposts/posts")
      .then((res) => res.json())
      .then(setPosts)
      .catch(handleError);
  }
});
Enter fullscreen mode Exit fullscreen mode

you can check the full example here

Any feedback is appreciated 🤝

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post →

Top comments (7)

Collapse
 
dennisfrijlink profile image
Dennis Frijlink

I don't think this is the best solution for a SPA but I like the idea and the simplicity!

 
matjones profile image
Mat Jones

Yeah it also means you have to send fully hydrated renders for every page in the entire app for it to work properly

Collapse
 
darcdev profile image
Diego Andres Rojas

Thanks bro, that method give me other ideas. 😁

Collapse
 
_faridjunior profile image
mohamed farid

you're welcome ♥

Collapse
 
_faridjunior profile image
mohamed farid • Edited

use hash in the URl to style,
build Demo with a few lines of code.

it is a way and you can use it in the right position

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️