1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaScriptで$_GETを実装する

Last updated at Posted at 2023-06-07

はじめに

JavaScriptでPHPの$_GETのようなことが出来る関数を作ったので紹介します。

関数

コピペしてお使いください。見ての通り、とても簡単です。

get.js
function $_GET(arg) {
    return new URL(location).searchParams.get(arg)
}

使い方

下のように使うことができます。PHPとほぼ同じですね。

main.js
const example = $_GET('example')

また、下のように書くことでデフォルト値を設定できます。これもPHPとほぼ同じですね。

main.js
const example = $_GET('example') ?? 'default'

おわりに

指摘、提案、その他のコメントは大歓迎です。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?