3つ目を足して仕上げる
最後の節です。棚検索のフォームを足して3つにし、見た目を整えて、 人に渡せる状態にします。
3つ目の足し方は2つ目とまったく同じなので、先に自分でやってみてください。
この節で作るもの
Section titled “この節で作るもの”棚検索が加わって3つになり、見た目も整います。これがこのハンズオンの最終的な成果物です。
今回さわるファイル:
index.htmlに3つ目のフォームと<style>を足す
前の節を終えていない人は、ここから始められます。
3つ目を足す
Section titled “3つ目を足す”棚検索は、書庫・キャビネット・ドロアの番号で場所から探すものです。
行き先は shelf.html、パラメータは3つです。
| 項目 | パラメータ名 | 参考値 |
|---|---|---|
| 書庫 | room | 第2書庫 |
| キャビネット | cabinet | C-03 |
| ドロア | drawer | 2段目 |
部署コードはありません。 棚の場所が決まれば部署は関係ないからです。 よく似た3つのフォームですが、項目の構成からして同じではありません。
手順は前の節と同じです。やることを並べると5つです。
- 「2. 全文検索」のかたまりをコピーして、下に貼り付ける
- 見出しを
<h2>3. 棚検索</h2>にする <form>のidをshelfForm、結果の<span>のidをshelfResultにする- 表の行を
room/cabinet/drawerの3行にし、idとforにはshelf-を付ける <script>の「全文検索」のかたまりをコピーして、フォーム・行き先・出力先の3か所を変える
できたら答え合わせです。HTML はこうなります。
2つ目の結果の <p> の下
155<h2>3. 棚検索</h2>156
157<form id="shelfForm">158 <table>159 <thead>160 <tr>161 <th>項目</th>162 <th>パラメータ名</th>163 <th>値</th>164 <th>参考値</th>165 </tr>166 </thead>167 <tbody>168 <tr>169 <td><label for="shelf-room">書庫</label></td>170 <td>room</td>171 <td><input id="shelf-room" name="room" required /></td>172 <td>第2書庫</td>173 </tr>174 <tr>175 <td><label for="shelf-cabinet">キャビネット</label></td>176 <td>cabinet</td>177 <td><input id="shelf-cabinet" name="cabinet" /></td>178 <td>C-03</td>179 </tr>180 <tr>181 <td><label for="shelf-drawer">ドロア</label></td>182 <td>drawer</td>183 <td><input id="shelf-drawer" name="drawer" /></td>184 <td>2段目</td>185 </tr>186 </tbody>187 </table>188
189 <p>190 <button type="submit">生成</button>191 </p>192</form>193
194<p>195 生成されたURL:<span id="shelfResult"></span>196</p>required が付くのは書庫だけです。書庫が決まらないと場所が特定できないからです。
これも3つのフォームで別々に決めています。
<script> はこうなります。
<script> の中。2つ目の }); の下
253// 棚検索254const shelfForm = document.getElementById('shelfForm');255const shelfResult = document.getElementById('shelfResult');256
257shelfForm.addEventListener('submit', (event) => {258 event.preventDefault();259
260 const params = new URLSearchParams();261
262 new FormData(shelfForm).forEach((value, name) => {263 if (value !== '') {264 params.append(name, value);265 }266 });267
268 const url = 'shelf.html?' + params.toString();269
270 const link = document.createElement('a');271 link.setAttribute('href', url);272 link.setAttribute('target', '_blank');273 link.textContent = url;274
275 shelfResult.textContent = '';276 shelfResult.append(link);277});行き先の shelf.html も、まだ自分のフォルダにありません。これまでと同じようにコピーします。
解凍してできる url-generator の中から、shelf.html だけを自分のフォルダにコピーしてください。
これで受け取り側が3つそろいます。
(自分の作業フォルダ)├── index.html 自分で書いているファイル├── docs.html 第2章で置いたもの├── fulltext.html 前の節で置いたもの└── shelf.html ← 今回あらたに置くもの項目が4つから3つに減っても、forEach の中は変わりません。
name が dept でも room でも、FormData は同じように集めます。
見た目を整える
Section titled “見た目を整える”動くものはできました。最後に <style> を足して読みやすくします。
<style> に書き足すだけなので、動きは何も変わりません。
まず、ページ全体と見出しです。
<style> の先頭
66<style>7 body {8 font-family: "Segoe UI", "Meiryo", sans-serif;9 line-height: 1.7;10 margin: 24px;11 }12
13 h1 {14 font-size: 1.4em;15 border-bottom: 2px solid #70ad47;16 padding-bottom: 4px;17 }18
19 h2 {20 font-size: 1.1em;21 margin-top: 2em;22 }23
724 table {825 border-collapse: collapse;926 }h2 の margin-top は、フォームどうしの間を空けるためのものです。
3つが続けて並んでいると、どこで区切れているのかが読み取りにくくなります。
次に、入力欄・ボタン・結果欄です。
<style> の終わり
1835 th {1936 background: #e2efda;2037 }38
39 input {40 width: 14em;41 }42
43 button {44 padding: 4px 16px;45 }46
47 .result {48 background: #f3f3f3;49 padding: 8px 10px;50 word-break: break-all;51 }2152</style>input の幅をそろえたのは、表の列の幅が行ごとに変わるのを防ぐためです。
.result は、class="result" が付いた要素に当たります。id と違って、
class は同じものを何個でも置けます。結果欄は3つあるので class を使います。
word-break: break-all は、長い URL を途中で折り返させる指定です。
これが無いと、URL が横に伸びて画面からはみ出します。
結果の <p> に class を付けます。3か所とも同じように直してください。
結果の <p>。3か所すべて
71<p>102<p class="result">72103 生成されたURL:<span id="docsResult"></span>73104</p>これで完成です。
1枚のファイルとして配る
Section titled “1枚のファイルとして配る”出来上がったものは4つのファイルです。
| ファイル | 役割 |
|---|---|
index.html | 作ったページ |
docs.html | 資料検索の受け取り側 |
fulltext.html | 全文検索の受け取り側 |
shelf.html | 棚検索の受け取り側 |
この4つを共有フォルダに置けば、ほかの人も開いて使えます。 インストールも設定も要りません。Edge でダブルクリックするだけです。
よく使う人には、index.html を Edge のお気に入りに入れてもらうか、
デスクトップにショートカットを作ってもらうと早く開けます。
行き先を本物の URL に差し替えたあとは、受け取り側の3枚は要らなくなります。
配るのは index.html 1枚だけになります。
サクラエディタで保存するときは、文字コードを UTF-8 にしてください。 別の文字コードで保存すると、Edge で開いたときに日本語が崩れます。
コラム: それでもまとめたくなったら
Section titled “コラム: それでもまとめたくなったら”<script> の中には、ほとんど同じかたまりが3つ並んでいます。
違うのは、前の節で見た3か所だけです。
3つ目を書いた時点で「またコピーした」と思ったなら、その感覚は正しいものです。 フォームが10個になれば、まとめる価値は確実に出てきます。 その場合は、違う3か所だけを並べたものを用意して、残りを1つの処理で回す形になります。
ただし、まとめるかどうかを決める材料は「似ているかどうか」ではありません。 3つが今後も同じ向きに変わるかです。
- 同じ向きに変わるなら、1か所直せば3つに効くので、まとめたほうが速い
- 別々に変わるなら、まとめた1か所に「資料検索のときだけ」という分岐が増えていく
この教材の3つは、後者になりやすいものです。 行き先のシステムが別々で、必須項目も違い、項目の数も違います。 全文検索に「語句が1つも無ければ止める」を足したくなったとき、 その判定は資料検索には要りません。
3つのうち2つが同じ向きに変わったことが実際に2〜3回あってから、 まとめる形を考えれば十分です。先にまとめておく必要はありません。
この教材のまとめ
Section titled “この教材のまとめ”入力した値から検索URLを作るページが、1枚のファイルとしてできました。
やったことは、結局3つです。
- 入力欄の値を取り出す(
id/name/FormData) - キーと値を URL に組み立てる(
URLSearchParams) - 押せるリンクにして画面に出す(
createElement/append)
第2章では1〜3を全部自分で書き、第3章でそれをブラウザ側に渡しました。 書く量が減ったぶん、第4章ではフォームを3つ並べるのが「コピーして3か所変える」で済みました。
パラメータの数が増えても、フォームが増えても、この形のまま足していけます。 自分の案件に移すときは、行き先の URL とパラメータ名を差し替えるところから始めてください。
index.html を Edge で開いて、次の4つを確かめてください。
- フォームが3つ並び、見出しの間に余白が空いている
- 3つの「生成」が、それぞれ
docs.html?/fulltext.html?/shelf.html?で始まる URL を作る - 長い URL が結果欄の中で折り返して、画面からはみ出さない
- 1つの「生成」を押しても、ほかの2つの結果欄は変わらない
棚検索で書庫を空にして押すと、ブラウザの吹き出しが出ます。
required も3つのフォームで別々に効いています。
この節を終えたときのディレクトリ
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>全文検索(受け取り側)</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> body { font-family: "Segoe UI", "Meiryo", sans-serif; line-height: 1.7; margin: 24px; }
h1 { font-size: 1.4em; border-bottom: 2px solid #70ad47; padding-bottom: 4px; }
h2 { font-size: 1.1em; margin-top: 2em; }
table { border-collapse: collapse; }
th, td { border: 1px solid #999; padding: 4px 10px; text-align: left; }
th { background: #e2efda; }
input { width: 14em; }
button { padding: 4px 16px; }
.result { background: #f3f3f3; padding: 8px 10px; word-break: break-all; } </style> </head> <body> <h1>検索URLを作る</h1>
<h2>1. 資料検索</h2>
<form id="docsForm"> <table> <thead> <tr> <th>項目</th> <th>パラメータ名</th> <th>値</th> <th>参考値</th> </tr> </thead> <tbody> <tr> <td><label for="docs-dept">部署コード</label></td> <td>dept</td> <td><input id="docs-dept" name="dept" required /></td> <td>25、ALL</td> </tr> <tr> <td><label for="docs-title">件名</label></td> <td>title</td> <td><input id="docs-title" name="title" /></td> <td>月次報告</td> </tr> <tr> <td><label for="docs-author">作成者</label></td> <td>author</td> <td><input id="docs-author" name="author" /></td> <td>山田</td> </tr> <tr> <td><label for="docs-date">作成日</label></td> <td>date</td> <td><input id="docs-date" name="date" type="date" /></td> <td>2026-04-01</td> </tr> </tbody> </table>
<p> <button type="submit">生成</button> </p> </form>
<p class="result"> 生成されたURL:<span id="docsResult"></span> </p>
<h2>2. 全文検索</h2>
<form id="fulltextForm"> <table> <thead> <tr> <th>項目</th> <th>パラメータ名</th> <th>値</th> <th>参考値</th> </tr> </thead> <tbody> <tr> <td><label for="fulltext-dept">部署コード</label></td> <td>dept</td> <td><input id="fulltext-dept" name="dept" required /></td> <td>25、ALL</td> </tr> <tr> <td><label for="fulltext-word1">検索語句(1)</label></td> <td>word1</td> <td><input id="fulltext-word1" name="word1" /></td> <td>安全</td> </tr> <tr> <td><label for="fulltext-word2">検索語句(2)</label></td> <td>word2</td> <td><input id="fulltext-word2" name="word2" /></td> <td>点検</td> </tr> <tr> <td><label for="fulltext-word3">検索語句(3)</label></td> <td>word3</td> <td><input id="fulltext-word3" name="word3" /></td> <td></td> </tr> </tbody> </table>
<p> <button type="submit">生成</button> </p> </form>
<p class="result"> 生成されたURL:<span id="fulltextResult"></span> </p>
<h2>3. 棚検索</h2>
<form id="shelfForm"> <table> <thead> <tr> <th>項目</th> <th>パラメータ名</th> <th>値</th> <th>参考値</th> </tr> </thead> <tbody> <tr> <td><label for="shelf-room">書庫</label></td> <td>room</td> <td><input id="shelf-room" name="room" required /></td> <td>第2書庫</td> </tr> <tr> <td><label for="shelf-cabinet">キャビネット</label></td> <td>cabinet</td> <td><input id="shelf-cabinet" name="cabinet" /></td> <td>C-03</td> </tr> <tr> <td><label for="shelf-drawer">ドロア</label></td> <td>drawer</td> <td><input id="shelf-drawer" name="drawer" /></td> <td>2段目</td> </tr> </tbody> </table>
<p> <button type="submit">生成</button> </p> </form>
<p class="result"> 生成されたURL:<span id="shelfResult"></span> </p>
<script> // 資料検索 const docsForm = document.getElementById('docsForm'); const docsResult = document.getElementById('docsResult');
docsForm.addEventListener('submit', (event) => { event.preventDefault();
const params = new URLSearchParams();
new FormData(docsForm).forEach((value, name) => { if (value !== '') { params.append(name, value); } });
const url = 'docs.html?' + params.toString();
const link = document.createElement('a'); link.setAttribute('href', url); link.setAttribute('target', '_blank'); link.textContent = url;
docsResult.textContent = ''; docsResult.append(link); });
// 全文検索 // 資料検索とよく似た処理だが、まとめずにそれぞれ書く。 // 行き先も項目も別物なので、片方を直してもう片方が壊れるのを避ける。 const fulltextForm = document.getElementById('fulltextForm'); const fulltextResult = document.getElementById('fulltextResult');
fulltextForm.addEventListener('submit', (event) => { event.preventDefault();
const params = new URLSearchParams();
new FormData(fulltextForm).forEach((value, name) => { if (value !== '') { params.append(name, value); } });
const url = 'fulltext.html?' + params.toString();
const link = document.createElement('a'); link.setAttribute('href', url); link.setAttribute('target', '_blank'); link.textContent = url;
fulltextResult.textContent = ''; fulltextResult.append(link); });
// 棚検索 const shelfForm = document.getElementById('shelfForm'); const shelfResult = document.getElementById('shelfResult');
shelfForm.addEventListener('submit', (event) => { event.preventDefault();
const params = new URLSearchParams();
new FormData(shelfForm).forEach((value, name) => { if (value !== '') { params.append(name, value); } });
const url = 'shelf.html?' + params.toString();
const link = document.createElement('a'); link.setAttribute('href', url); link.setAttribute('target', '_blank'); link.textContent = url;
shelfResult.textContent = ''; shelfResult.append(link); }); </script> </body></html><!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>