Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
URL location can someone explain?

I am just starting to learn location API(s), such as redirection URL in javascript and I can not understand the following three lines, Can someone explain me?

let windowUrl = new URLSearchParams(window.location.search);
const queryString = window.location.href;
const firstParam = queryString.substring(queryString.lastIndexOf("?") + 1).split("=")[0];
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The first line is useless. As you can see, windowUrl never gets used.

The following two lines:

window.location.href is nothing but the URL that you see in your browser's location bar. Say, https://www.youtube.com/watch?v=123456

so queryString = "https://www.youtube.com/watch?v=123456"

what the 2nd line does is to take everything that comes after "?" in that string. So v=123456

Then, it splits v=123456 by "=" as a separator. So, finally you get 123456.

Now, all of the above is quite barbaric, as you could obtain the value that "v" parameter this way:

let url = new URL(window.location.href);
let v   = url.searchParams.get("v");

URL is an interface that will, shall we say "analyze" a URL and give you methods to parse it conveniently, such as the searchParams method, and more.

MDN https://developer.mozilla.org/en-US/docs/Web/API/URL

about 4 years ago · Juan Pablo Isaza Report

0

let windowUrl = new URLSearchParams(window.location.search) ==> fetching URL params from the url. E.g website.com/?hello=1

const queryString = window.location.href; ===> getting the URL of your current page.

const firstParam = queryString.substring(queryString.lastIndexOf("?") + 1).split("=")[0]; ===> String manipulation to get the first param from the URL. Would open dev tool and play with it, to get the feeling.

E.g:

const x = "https://stackoverflow.com/questions/69835761/js-url-location-can-someone-explain/?hello=1"
const x = "https://stackoverflow.com/questions/69835761/js-url-location-can-someone-explain/?hello=1"
console.log(x)

Result: 'hello'

about 4 years ago · Juan Pablo Isaza Report

0

Description inline the code:

// for Example: https://stackoverflow.com/questions/69835761/js-url-location-can-someone-explain?search=hello

windowUrl = new URLSearchParams(window.location.search);
// URLSearchParams object that helps you to provide data to the query url
// window.location.search will return the current search query from your url

queryString = window.location.href;
// the current url: in this case: https://stackoverflow.com/questions/69835761/js-url-location-can-someone-explain?search=hello

firstParam = queryString.substring(queryString.lastIndexOf("?") + 1).split("=")[0]
// you filter or prase the value from the query string

But you can have it easier, like @resle already wrote.

// get the current url
const uri = new URL(window.location.href);
const quersValue = uri.searchParams.get("search");
// output : hello
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!