Query Parameters in JavaScript (3+1 Ways)

  Рет қаралды 19,780

DevTips

DevTips

Күн бұрын

Пікірлер: 48
@OfficialDevTips
@OfficialDevTips 6 жыл бұрын
That cool inline evaluation thing is called *Quokka* quokka.devtipsshow.com
@normanperrin6491
@normanperrin6491 6 жыл бұрын
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 6 жыл бұрын
i was about to comment this, using URLSearchParams.entries() you get an iterator with all the key/value pairs
@OfficialDevTips
@OfficialDevTips 6 жыл бұрын
Thanks for sharing! Great ideas!
@mazyvan
@mazyvan 6 жыл бұрын
I thought the same
@shgysk8zer0
@shgysk8zer0 6 жыл бұрын
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 6 жыл бұрын
Done as long as it's acceptable to crash all versions of IE.
@mazyvan
@mazyvan 6 жыл бұрын
@@MichaelSalo there's one thing called transpilers
@MichaelSalo
@MichaelSalo 6 жыл бұрын
@@mazyvan My understanding is URL object is a browser API not a JS API that would normally be transpiled.
@jameshamann465
@jameshamann465 6 жыл бұрын
You need a polyfill for Internet Explorer
@RomanSteiner_xD
@RomanSteiner_xD 6 жыл бұрын
@@MichaelSalo I guess if he uses `URLSearchParams`, he could also use `URL`. Neither work on IE.
@cspear888
@cspear888 6 жыл бұрын
Can you also teach how we design our own query parameters?
@mattcroat
@mattcroat 6 жыл бұрын
You're a treasure David.
@Xizmoify
@Xizmoify 6 жыл бұрын
Not sure if it fits this channel, but perhaps working with node.js and some websockets. Or perhaps building your own npm module
@matthewbarbara2916
@matthewbarbara2916 6 жыл бұрын
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 6 жыл бұрын
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/
@AlvarLagerlof
@AlvarLagerlof 6 жыл бұрын
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"])
@MightyArts
@MightyArts 6 жыл бұрын
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 6 жыл бұрын
It’s called quokka. Link in the description.
@jasonjara3453
@jasonjara3453 6 жыл бұрын
It's Quokka. quokkajs.com/
@MightyArts
@MightyArts 6 жыл бұрын
@@OfficialDevTips Thanks a lot for the reply! Love your videos :D
@HashimWarren
@HashimWarren 6 жыл бұрын
why does a.search only produce what's after the ? mark
@HashimWarren
@HashimWarren 6 жыл бұрын
oh, ok - the search method on a string returns everything after the question mark
@mykolasenechyn8177
@mykolasenechyn8177 6 жыл бұрын
How are getting variable values inline in your code?
@OfficialDevTips
@OfficialDevTips 6 жыл бұрын
It’s called quokka. Link in the description.
@mykolasenechyn8177
@mykolasenechyn8177 6 жыл бұрын
@@OfficialDevTips Thanks!
@md.akib5124
@md.akib5124 6 жыл бұрын
wow man you are a genius
@pureretro5979
@pureretro5979 6 жыл бұрын
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 6 жыл бұрын
Yeah. Probably it would have been better to show off another way of doing it, the one you suggest is good.
@MrPDTaylor
@MrPDTaylor 6 жыл бұрын
I just learned this for the first time earlier today! Mere coincidence, I think not!
@zeocamo
@zeocamo 6 жыл бұрын
Why not just use the new Url() ??? This 3 ways look like walking over the sea to get water
@yassine_klilich
@yassine_klilich 6 жыл бұрын
great solution using DOM API (y)
@Dr3amDisturb3r
@Dr3amDisturb3r 6 жыл бұрын
Eeyyy new intro! Now some sound effects please! :)
@kirillpavlovskii8342
@kirillpavlovskii8342 3 жыл бұрын
Nice
@tekushinio
@tekushinio 6 жыл бұрын
hej monika XDD
@HeeyStrong
@HeeyStrong 6 жыл бұрын
Good to know the good ways of querying those params, thanks lel
@OfficialDevTips
@OfficialDevTips 6 жыл бұрын
Haha I appreciate someone finds this video useful. Sometimes things you yourself stumble upon are not that interesting to other people. 🙀😅
@SamzehGFX
@SamzehGFX 6 жыл бұрын
I created a js micro library for parameter manipulation. Helped me a bunch on some recent projects... github.com/samb97/pram.js
@UniBreakfast
@UniBreakfast 5 жыл бұрын
God, the muar from your sweater is painful.)))
@mohamedmorh
@mohamedmorh 6 жыл бұрын
keep on hacking :D
@zeocamo
@zeocamo 6 жыл бұрын
Why not just use the new Url() ??? This 3 ways look like walking over the sea to get water
What is a QueryString?
13:00
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 14 М.
Critical CSS + Fail 🧨
35:37
DevTips
Рет қаралды 14 М.
When you have a very capricious child 😂😘👍
00:16
Like Asiya
Рет қаралды 18 МЛН
1% vs 100% #beatbox #tiktok
01:10
BeatboxJCOP
Рет қаралды 67 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
Create and Read Query Strings (URL parameters) - JavaScript Tutorial
11:55
Vim Tips I Wish I Knew Earlier
23:00
Sebastian Daschner
Рет қаралды 88 М.
How to Code (almost) Any Feature
9:48
DaFluffyPotato
Рет қаралды 714 М.
Regular Expressions in JavaScript - #1 REGEX ULTRA BASICS
23:16
Solving one of PostgreSQL's biggest weaknesses.
17:12
Dreams of Code
Рет қаралды 225 М.
How to Learn to Code FAST (Do This or Keep Struggling)
11:00
Andy Sterkowitz
Рет қаралды 725 М.
The Async Await Episode I Promised
12:04
Fireship
Рет қаралды 1,1 МЛН
Always Check for the Hidden API when Web Scraping
11:50
John Watson Rooney
Рет қаралды 660 М.