Getting a URL Parameter in Javascript | by Joseph Maurer | Geek Tradition

Getting a URL Parameter in Javascript | by Joseph Maurer | Geek Tradition


When programming in Javascript there are occasions while you would possibly need to know if there have been any parameters handed by way of the URL. In case you aren’t aware of URL parameters, they’re the arguments set after the bottom URL and the ‘?’. For instance let’s have a look at the under instance:

https://www.josephamaurer.com/Tasks/DevAPI/RecentDevPosts.html?PerPage=100

?PerPage=100 is the primary parameter that’s handed with this URL. It’s as much as your javascript logic to seize this parameter’s worth and use it appropriately. So can there be a number of parameters? You betcha! Further parameters are added with the next syntax:

https://www.josephamaurer.com/Tasks/DevAPI/RecentDevPosts.html?PerPage=100&Web page=2

‘&Web page=2’ is the second parameter that’s with this URL. At this level you is perhaps questioning what are the restrictions of passing arguments like this? Effectively the apparent one is you could’t have any areas. One other is that the ‘#’ character is reserved for leaping to a bit of a doc. Typically, URL Encoding is used to cope with this drawback and make any textual content secure to move within the URL. Web Explorer (RIP) had a most size of 2,083 characters. Though, the RFC 2616 spec says that servers want to have the ability to deal with any variety of characters, you do threat an online server failing to reply if the request is simply too lengthy. I extremely advocate wanting on the Google Chrome pointers if you happen to plan to do that on a manufacturing mission.

The most typical use of those question strings inside a URL is net varieties. When a consumer hits submit on a type, their responses are posted within the URL for processing by the backend. On this tutorial, we’ll give attention to simply grabbing values from that URL for processing, however there are many examples of utilizing this on the backend.

When utilizing javascript to parse the URL, it’s best to make use of URLSearchParams as a substitute of making an attempt to parse the string your self. You possibly can use common expressions to strive to do that, however as I wrote earlier this can be a horrible thought. So let’s have a look at the instance under and see how this works.

As you possibly can see, grabbing the params is definitely a very easy course of. You may very simply question to see what objects are current and verify if they’re null or an precise worth first earlier than utilizing them. On the time of penning this, browser assist is superb and is on the market to make use of nearly in all places.

Constructing on my final video put up, now you can move parameters to my instance web page to play with the variety of posts that it returns.

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.