Using JS to redirect instead of anchor tag
The
makeCard()
basically returns a div thats wrapped in an anchor tag.
And I am adding click event listener to all the anchor tags cause i have to store the id in local storage of the card that has been clicked .
This works but idk why something feels sketchy about this to me. Idk what. Does this approach have any drawback?21 Replies
yes: a right-click will record the information to the localstorage
and too many event listeners can cause some performance issues
how about you store the id once you visit the page?
also, you're setting the same key with the value, which is going to always get overwritten when you open a new tab
hmm.. Ig that can be solved through event delegation no?
kinda
you still need to check which button was clicked, with this approach
also, a click doesn't mean a visit
so, still a bad idea
hmm.. lemme first explain you what m doing, basically I have a projects.html page where all my projects r being shown as cards (same ones returned by makeCard() , and each card have an id)
now when a card is clicked it redirects me to projectDes.html where it shows the title and the description of the project. and i am using the id to get the corresponding projects description and title from the array that contains objects which basically represents each project. keys: title, id, description
So essentially when a project is clicked I need to show a description of the project in the following page, so I need to know which card was clicked, hence m using id as a source for that
still a terrible idea
hmm
so what approach you reckon would be a better choice
just use the
#
part of the url to pass state
or use query parameters
both would be far superior to whatever you have
and guess what? it works without attaching event listeners all over and storing unreliable data in the localstoragelike getting it from the url?
yes
as you should have implemented in the first place
I mean the thing is , the code I wrote was to help someone, that someone is trying to achieve it through JUST js
cause they r a beginner
the query param , I'll need php for that no?
or wait no
location.href
but how do I add the id to the url?
the only way Ik is when u use get method it adds it to the url
no, you can read from the url
you add it in html
The term is called query parameter? Asking so I can look it up
you can even be smart and not switch page at all, and just show the extra information in a dialog
and that information is stored in an object
it sounds like this is for a portfolio or something, so, this is a much better approach
also, it saves network requests
I mean true, but since the person that m tryna help wants to do it through a separate page I can't do that
xD
But query param seems like a solid approach
the whole point is to basically share a state from 1 page to another
you can use query parameters or pass an id through the hash
k I'll take a look into query parameters first
that's advanced
you don't share state
but sharing some information about what you want, that's easy
the info about which card was clicked, does that consider as a state?
ig not
i don't
that's a request
hmm fair
but ya I'll take a look into query parameters now
thank you brother
you're welcome