1

これは、キーワードを入力して検索エンジンを選択し、[検索] ボタンを押して検索する小さなプログラムのコードです。しかし、Googleは私をPOSTに任せません。他に何ができますか?

編集: Yahoo と Bing は正常に動作します。

エラー

405. That’s an error.

The request method POST is inappropriate for the URL 
/search?q=computer. That’s all we know. 

HTML

<form name="search" action="" method="Post" onSubmit="redirect()">
<input type="text" name="keyword"><br />
Google<input type="radio" name="ch" checked>
Yahoo!<input type="radio" name="ch">
Bing<input type="radio" name="ch"><br />
<input type="submit" value="Search">
</form>

Javascript

<script type="text/javascript">
var searchengine=[
"http://google.com/search?q=",
"http://search.yahoo.com/search?p=",
"http://bing.com/search?q="
];

function redirect()
{
    var radioButtons = document.getElementsByName("ch");
    for (var x = 0; x < radioButtons.length; x++) {
        if (radioButtons[x].checked)
        {
            document.search.action = searchengine[x] + document.search.keyword.value;
        }
    }
}
</script>
4

2 に答える 2

4

しかし、グーグルは私をPOSTに任せないでください。他に何ができますか?

GETフォームではなく使用するかPOST、関連するURLをに割り当てますwindow.location

後者の例を次に示します。その他の変更:

  • いくつかを追加しましlabelた。
  • 選択したラジオボタンのマッチング方法を変更し、searchengineより堅牢で保守しやすくしました。
  • 検索フォームの名前を変更しました。これはオブジェクトにダンプされるので、window「検索」のような単純な単語は避けます。
  • キーワードを適切にエンコードしました(URIパラメーターをエンコードする必要があります)。

ライブコピー| ライブソース

HTML:

<form name="searchForm" action="" method="GET" onSubmit="return doSearch()">
<input type="text" name="keyword">
  <br>
  <label>Google<input type="radio" name="ch" value="google" checked></label>
  <label>Yahoo!<input type="radio" name="ch" value="yahoo"></label>
  <label>Bing<input type="radio" name="ch" value="bing"></label>
  <br>
  <input type="submit" value="Search">
</form>

JavaScript:

var searchengine = {
  "google": "http://google.com/search?q=",
  "yahoo": "http://search.yahoo.com/search?p=",
  "bing": "http://bing.com/search?q="
};
function doSearch() {
  var frm, index, cb;

  frm = document.searchForm;
  if (frm && frm.ch) {
    if (frm.ch) {
      for (index = 0; index < frm.ch.length; ++index) {
        cb = frm.ch[index];
        if (cb.checked) {
          window.location = searchengine[cb.value] +
            encodeURIComponent(frm.keyword.value);
        }
      }
    }
  }

  return false; // Cancels form submission
}
于 2012-10-03T08:44:15.983 に答える
0

"http:google.com/search?q="、正しくフォーマットされていません..

試す"http://google.com/search?q="

于 2012-10-03T08:43:42.863 に答える