パラメータを増やす
件名だけでは検索条件として足りません。部署コード・件名・作成者・作成日の4項目にします。 項目が増えると、区切り文字の扱いと空欄の扱いという2つの面倒が出てきます。
この節で作るもの
Section titled “この節で作るもの”項目が4つになり、表で並びます。入力した項目だけが URL に入ります。
今回さわるファイル:
index.htmlに入力欄を3つ足す
前の節を終えていない人は、ここから始められます。
項目を4つにする
Section titled “項目を4つにする”入力欄が4つになるので、表に並べます。項目名・キー・入力欄・参考値の4列にしておくと、 使う人が「どこに何を入れるか」を迷いません。
表の見た目を整えるために <style> も足します。<head> の中、<title> の下に書きます。
<head> の中
6<style>7 table {8 border-collapse: collapse;9 }10
11 th,12 td {13 border: 1px solid #999;14 padding: 4px 10px;15 text-align: left;16 }17
18 th {19 background: #e2efda;20 }21</style>入力欄の <p> を、表に置き換えます。
<h1> の下。これまでの件名の <p> と入れ替える
26<table>27 <thead>28 <tr>29 <th>項目</th>30 <th>パラメータ名</th>31 <th>値</th>32 <th>参考値</th>33 </tr>34 </thead>35 <tbody>36 <tr>37 <td>部署コード</td>38 <td>dept</td>39 <td><input id="dept" /></td>40 <td>25、ALL</td>41 </tr>42 <tr>43 <td>件名</td>44 <td>title</td>45 <td><input id="title" /></td>46 <td>月次報告</td>47 </tr>48 <tr>49 <td>作成者</td>50 <td>author</td>51 <td><input id="author" /></td>52 <td>山田</td>53 </tr>54 <tr>55 <td>作成日</td>56 <td>date</td>57 <td><input id="date" /></td>58 <td>2026-04-01</td>59 </tr>60 </tbody>61</table>value="月次報告" は外しました。入れてほしい値は「参考値」の列で示せるので、
入力欄は空から始めます。
<script> の先頭も、増えた入力欄に合わせます。
<script> の先頭
24const titleInput = document.getElementById('title');72const deptInput = document.getElementById('dept');73const titleInput = document.getElementById('title');74const authorInput = document.getElementById('author');75const dateInput = document.getElementById('date');2576const generateButton = document.getElementById('generateButton');2677const result = document.getElementById('result');「&」でつなぐ
Section titled “「&」でつなぐ”パラメータを2つ以上渡すときは & で区切ります。ただし、使う区切り文字は位置によって違います。
そこで、? から後ろの部分(クエリ文字列といいます)を変数に組み立ててから、
最後にベース URL とつなぐ形にします。
<script> のクリック処理
2879generateButton.addEventListener('click', () => {29 const url = 'docs.html?title=' + titleInput.value;80 let query = '';81
82 query = query + 'dept=' + deptInput.value;83 query = query + '&title=' + titleInput.value;84 query = query + '&author=' + authorInput.value;85 query = query + '&date=' + dateInput.value;86
87 const url = 'docs.html?' + query;3088
3189 result.innerHTML = '<a href="' + url + '" target="_blank">' + url + '</a>';3290});const ではなく let を使いました。query は4回書き換えるので、
あとから代入し直せる let が必要です。
空欄のまま押すとどうなるか
Section titled “空欄のまま押すとどうなるか”4項目すべてを埋めて押せば、思ったとおりの URL が出ます。 では部署コードだけ入れて、ほかを空のまま押してみてください。
title=&author=&date= が付いた URL ができます。リンクを押して受け取り側を見ると、
空の値が3つ並んでいます。受け取り側には「空文字で検索せよ」と伝わってしまうので、
入力されていない項目はそもそも URL に入れないのが正しい形です。
空欄を飛ばす
Section titled “空欄を飛ばす”if で「入力されているものだけ足す」に変えます。
<script> のクリック処理
2879generateButton.addEventListener('click', () => {2980 let query = '';3081
31 query = query + 'dept=' + deptInput.value;32 query = query + '&title=' + titleInput.value;33 query = query + '&author=' + authorInput.value;34 query = query + '&date=' + dateInput.value;82 if (deptInput.value !== '') {83 if (query !== '') {84 query = query + '&';85 }86 query = query + 'dept=' + deptInput.value;87 }88
89 if (titleInput.value !== '') {90 if (query !== '') {91 query = query + '&';92 }93 query = query + 'title=' + titleInput.value;94 }3595
3696 const url = 'docs.html?' + query;author と date も同じ形で書き足してください。4つとも同じ形の if が並びます。
「?」と「&」の使い分けが面倒になる
Section titled “「?」と「&」の使い分けが面倒になる”内側の if に注目してください。
if (query !== '') { query = query + '&';}空欄を飛ばせるようにしたせいで、「いま足そうとしているものが先頭かどうか」が 実行してみるまで決まらなくなりました。部署コードが空なら件名が先頭になり、 件名も空なら作成者が先頭になります。
そのため、1つ足すたびに「もう何か入っているか」を確かめる必要があります。
項目が4つなら if が8個、10項目なら20個です。
書けば動きますが、本来やりたいのは「キーと値を足していく」ことだけのはずです。 第3章で、この区切り文字の面倒をまとめて手放します。
4項目のうちいくつかだけ入力して「生成」を押し、入力した項目だけが URL に入っていれば成功です。
この節を終えたときのディレクトリ
Section titled “この節を終えたときのディレクトリ”<!doctype html><html lang="ja"> <head> <meta charset="utf-8" /> <title>資料検索(受け取り側)</title> <style> body { font-family: "Segoe UI", "Meiryo", sans-serif; line-height: 1.7; margin: 24px; } table { border-collapse: collapse; } th, td { border: 1px solid #999; padding: 4px 10px; text-align: left; } th { background: #e2efda; } .note { color: #555; } .url { word-break: break-all; } </style> </head> <body> <h1>資料検索(受け取り側)</h1>
<p class="note"> 検索システムの代わりに置いてある、受け取ったパラメータを並べるだけのページです。 実際に使うときは、ここが本物の検索システムのURLに変わります。 </p>
<p>受け取ったURL:<span id="url" class="url"></span></p>
<h2>受け取ったパラメータ</h2>
<table> <thead> <tr> <th>キー</th> <th>値</th> </tr> </thead> <tbody id="rows"></tbody> </table>
<p id="empty" class="note" hidden>パラメータは1つも付いていません。</p>
<script> const url = document.getElementById('url'); const rows = document.getElementById('rows'); const empty = document.getElementById('empty');
url.textContent = location.href;
// location.search は「?」から後ろの文字列。 // 受け取る側も URLSearchParams で読み解くのがいちばん簡単。 const params = new URLSearchParams(location.search);
let count = 0;
params.forEach((value, key) => { const row = document.createElement('tr'); const keyCell = document.createElement('td'); const valueCell = document.createElement('td');
keyCell.textContent = key; valueCell.textContent = value;
row.append(keyCell, valueCell); rows.append(row);
count = count + 1; });
if (count === 0) { empty.hidden = false; } </script> </body></html><!doctype html><html lang="ja"> <head> <meta charset="utf-8" /> <title>資料検索のURLを作る</title> <style> table { border-collapse: collapse; }
th, td { border: 1px solid #999; padding: 4px 10px; text-align: left; }
th { background: #e2efda; } </style> </head> <body> <h1>資料検索のURLを作る</h1>
<table> <thead> <tr> <th>項目</th> <th>パラメータ名</th> <th>値</th> <th>参考値</th> </tr> </thead> <tbody> <tr> <td>部署コード</td> <td>dept</td> <td><input id="dept" /></td> <td>25、ALL</td> </tr> <tr> <td>件名</td> <td>title</td> <td><input id="title" /></td> <td>月次報告</td> </tr> <tr> <td>作成者</td> <td>author</td> <td><input id="author" /></td> <td>山田</td> </tr> <tr> <td>作成日</td> <td>date</td> <td><input id="date" /></td> <td>2026-04-01</td> </tr> </tbody> </table>
<p> <button id="generateButton">生成</button> </p>
<p> 生成されたURL:<span id="result"></span> </p>
<script> const deptInput = document.getElementById('dept'); const titleInput = document.getElementById('title'); const authorInput = document.getElementById('author'); const dateInput = document.getElementById('date'); const generateButton = document.getElementById('generateButton'); const result = document.getElementById('result');
generateButton.addEventListener('click', () => { // 「?」から後ろの部分(クエリ文字列)を1つずつ足していく。 let query = '';
// 2つ目以降の前には「&」が必要なので、 // すでに何か入っているかを毎回確かめることになる。 if (deptInput.value !== '') { if (query !== '') { query = query + '&'; } query = query + 'dept=' + deptInput.value; }
if (titleInput.value !== '') { if (query !== '') { query = query + '&'; } query = query + 'title=' + titleInput.value; }
if (authorInput.value !== '') { if (query !== '') { query = query + '&'; } query = query + 'author=' + authorInput.value; }
if (dateInput.value !== '') { if (query !== '') { query = query + '&'; } query = query + 'date=' + dateInput.value; }
const url = 'docs.html?' + query;
result.innerHTML = '<a href="' + url + '" target="_blank">' + url + '</a>'; }); </script> </body></html>