LoginSignup
1
0

More than 3 years have passed since last update.

メモ:超簡単にURLパラメータ事前入力

Posted at

コード

window.onload = function(){
  const searchParams = new URLSearchParams(decodeURI(location.search));
  var input = document.getElementById('input');
  input.value = searchParams.get('q');
  window.history.replaceState(0, 0,'./');
};

内容

ページ読み込み時に実行

window.onload = function(){
  //処理
};

デコードしたURLからパラメータを取得

const searchParams = new URLSearchParams(decodeURI(location.search));

qというパラメータの値をinputにセット

var input = document.getElementById('input');
input.value = searchParams.get('q');

ブラウザのURL欄からパラメータを削除

window.history.replaceState(0, 0,'./');
1
0
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
0