Тёмный

Query Parameters in JavaScript (3+1 Ways) 

DevTips
Подписаться 351 тыс.
Просмотров 20 тыс.
50% 1

Опубликовано:

 

28 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 48   
@OfficialDevTips
@OfficialDevTips 5 лет назад
That cool inline evaluation thing is called *Quokka* quokka.devtipsshow.com
@normanperrin6491
@normanperrin6491 5 лет назад
Instead of creating an `` element, you could use `new URLSearchParams(window.location.search)`. Also in node you could require the `url` lib and use it with `URLSearchParams` or `URL`. If you don't want to split the string yourself you could initialize an `URL` and then access the `url.search` for the search string, or `searchParams` for access the interface of `URLSearchParams`.
@nicoregules
@nicoregules 5 лет назад
i was about to comment this, using URLSearchParams.entries() you get an iterator with all the key/value pairs
@OfficialDevTips
@OfficialDevTips 5 лет назад
Thanks for sharing! Great ideas!
@mazyvan
@mazyvan 5 лет назад
I thought the same
@mattcroat
@mattcroat 5 лет назад
You're a treasure David.
@zeocamo
@zeocamo 5 лет назад
Why not just use the new Url() ??? This 3 ways look like walking over the sea to get water
@pureretro5979
@pureretro5979 5 лет назад
Splitting the new URL on the question mark in the 3rd example seemed like a quick solution (although one I'd probably use myself) rather than rebuild the URL from the location.protocol, host and pathname parts. Still, all these options work so it's all good. :-)
@OfficialDevTips
@OfficialDevTips 5 лет назад
Yeah. Probably it would have been better to show off another way of doing it, the one you suggest is good.
@Dr3amDisturb3r
@Dr3amDisturb3r 5 лет назад
Eeyyy new intro! Now some sound effects please! :)
@MightyArts
@MightyArts 5 лет назад
Hey quick question, what's the name of the VSCode extension that it tells you the value / type etc. of an element or variable when you do //? ? Thanks in advance :D
@OfficialDevTips
@OfficialDevTips 5 лет назад
It’s called quokka. Link in the description.
@jasonjara3453
@jasonjara3453 5 лет назад
It's Quokka. quokkajs.com/
@MightyArts
@MightyArts 5 лет назад
@@OfficialDevTips Thanks a lot for the reply! Love your videos :D
@HeeyStrong
@HeeyStrong 5 лет назад
Good to know the good ways of querying those params, thanks lel
@OfficialDevTips
@OfficialDevTips 5 лет назад
Haha I appreciate someone finds this video useful. Sometimes things you yourself stumble upon are not that interesting to other people. 🙀😅
@UniBreakfast
@UniBreakfast 5 лет назад
God, the muar from your sweater is painful.)))
@shgysk8zer0
@shgysk8zer0 5 лет назад
I knew this wouldn't have the real JavaScript solution as soon as I saw `const URL`. Best JavaScript solution: const url = new URL('...'); url.searchParams.delete('gclid'); Done.
@MichaelSalo
@MichaelSalo 5 лет назад
Done as long as it's acceptable to crash all versions of IE.
@mazyvan
@mazyvan 5 лет назад
@@MichaelSalo there's one thing called transpilers
@MichaelSalo
@MichaelSalo 5 лет назад
@@mazyvan My understanding is URL object is a browser API not a JS API that would normally be transpiled.
@jameshamann465
@jameshamann465 5 лет назад
You need a polyfill for Internet Explorer
@RomanSteiner_xD
@RomanSteiner_xD 5 лет назад
@@MichaelSalo I guess if he uses `URLSearchParams`, he could also use `URL`. Neither work on IE.
@SamzehGFX
@SamzehGFX 5 лет назад
I created a js micro library for parameter manipulation. Helped me a bunch on some recent projects... github.com/samb97/pram.js
@tekushinio
@tekushinio 5 лет назад
hej monika XDD
@cspear888
@cspear888 5 лет назад
Can you also teach how we design our own query parameters?
@kirillpavlovskii8342
@kirillpavlovskii8342 3 года назад
Nice
@AlvarLagerlof
@AlvarLagerlof 5 лет назад
How about this? You can extract anything after. const url = "test.domain?filter=hardcover&gclid=" const params = new Object() url .split("?")[1] .split("&") .forEach(param => { const [key, value] = param.split("=") params[key] = value }) console.log(params["filter"])
@Xizmoify
@Xizmoify 5 лет назад
Not sure if it fits this channel, but perhaps working with node.js and some websockets. Or perhaps building your own npm module
@md.akib5124
@md.akib5124 5 лет назад
wow man you are a genius
@zeocamo
@zeocamo 5 лет назад
Why not just use the new Url() ??? This 3 ways look like walking over the sea to get water
@MrPDTaylor
@MrPDTaylor 5 лет назад
I just learned this for the first time earlier today! Mere coincidence, I think not!
@matthewbarbara2916
@matthewbarbara2916 5 лет назад
Each time I needed query params to be allowed in the URL I've always used ?something=value. However, few months ago I needed to convert a JSON object into a query parameter to be passed in the URL and my colleague asked me why would I use such technique to pass the object in the URL rather than having a stringified JSON object pasted in the URL directly. Whilst this sound very ugly to me I could not really provide him an answer more than that most websites pass parameters in the url with ?something=value. What would be the drawback to have example.com/'{"something":"value"}" over the more common practise of ?something=value ?
@OfficialDevTips
@OfficialDevTips 5 лет назад
You have to escape the characters (so not you happen to have a ”/” that the browser thinks is a path). So a { becomes %7B. Each necessary character in the JSON format takes unnecessary space and you risk of oretty fast rinning into the length limit of urls. See this post blogs.dropbox.com/developers/2015/03/json-in-urls/
@yassine_klilich
@yassine_klilich 5 лет назад
great solution using DOM API (y)
@HashimWarren
@HashimWarren 5 лет назад
why does a.search only produce what's after the ? mark
@HashimWarren
@HashimWarren 5 лет назад
oh, ok - the search method on a string returns everything after the question mark
@mohamedmorh
@mohamedmorh 5 лет назад
keep on hacking :D
@mykolasenechyn8177
@mykolasenechyn8177 5 лет назад
How are getting variable values inline in your code?
@OfficialDevTips
@OfficialDevTips 5 лет назад
It’s called quokka. Link in the description.
@mykolasenechyn8177
@mykolasenechyn8177 5 лет назад
@@OfficialDevTips Thanks!
Далее
JavaScript Security: Hide your Code?
9:59
Просмотров 416 тыс.
JavaScript Pro Tips - Code This, NOT That
12:37
Просмотров 2,5 млн
Brilliant Budget-Friendly Tips for Car Painting!
00:28
Critical CSS + Fail 🧨
35:37
Просмотров 14 тыс.
Vim Tips I Wish I Knew Earlier
23:00
Просмотров 67 тыс.
The Async Await Episode I Promised
12:04
Просмотров 1,1 млн
My New Favorite Pagination Method
5:45
Просмотров 83 тыс.
How good are class arrow functions in JavaScript?
31:31