---
slug: "jsでurlをパースしてクエリ引数を取得する"
title: "Parsing URLs and Retrieving Query Parameters in JavaScript"
description: "The code `new URL(location.href).searchParams.get('q')` is a JavaScript expression used to retrieve the value of the query parameter `q` from the current page's URL. Here is a step-by-step breakdown of what this code does:"
url: "https://www.ytyng.com/en/blog/jsでurlをパースしてクエリ引数を取得する"
publish_date: "2017-03-02T05:50:34Z"
created: "2017-03-02T05:50:34Z"
updated: "2026-02-26T04:13:26.540Z"
categories: ["Javascript"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/76af76e95fbe4b2891031256ee91f84f.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# Parsing URLs and Retrieving Query Parameters in JavaScript

The code `new URL(location.href).searchParams.get('q')` is a JavaScript expression used to retrieve the value of the query parameter `q` from the current page's URL. Here is a step-by-step breakdown of what this code does:

1. `location.href` gets the full URL of the current page.
2. `new URL(location.href)` creates a new URL object using the current page's URL.
3. `.searchParams` accesses the URLSearchParams object associated with the URL, which allows you to work with the query string of the URL.
4. `.get('q')` retrieves the value of the query parameter named `q` from the URL's query string.

For example, if the current page's URL is `https://example.com/page?q=searchterm`, executing this code will return the string `"searchterm"`.
