自分用備忘録。
RESTfulなURL体系のサイトで検索機能をもたせるときに、HTMLとJSをどう作るか。
テキストボックスを作ります。
テキストボックスに入れた単語を、指定のパス以下にURLエンコードして展開します
テキストボックスでEnterキーを押す、もしくはsearchボタンを押すと検索のURLに移動
(KeyPressイベントを使わないのはIEとmozillaで動作が違うから)
以下、サンプルソース。
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
</head>
<body>
<script language="JavaScript">
<!--
function doSearch()
{
a=document.search.keyword.value;
coded = encodeURIComponent (a);
if(a.length>0) {
document.location='/search/' + coded;
return false;
}else{
return false;
}
}
//-->
</script>
</script>
<form name="search" onSubmit="return doSearch()">
<input name="keyword" type="text" />
<input type="button" value="search" onClick="doSearch()"/>
</form>
</body>
</html>