{"id":307593,"date":"2020-04-28T15:10:59","date_gmt":"2020-04-28T22:10:59","guid":{"rendered":"https:\/\/css-tricks.com\/?p=307593"},"modified":"2020-04-28T15:11:00","modified_gmt":"2020-04-28T22:11:00","slug":"how-to-redirect-a-search-form-to-a-site-scoped-google-search","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/how-to-redirect-a-search-form-to-a-site-scoped-google-search\/","title":{"rendered":"How to Redirect a Search Form to a Site-Scoped Google Search"},"content":{"rendered":"\n

This is just a tiny little trick that might be helpful on a site where you don’t have the time or desire to build out a really good on-site search solution. Google.com itself can perform searches scoped to one particular site. The trick is getting people there using that special syntax without them even knowing it.<\/p>\n\n\n\n\n\n\n\n

Make a search form:<\/p>\n\n\n\n

<form action=\"https:\/\/google.com\/search\" target=\"_blank\" type=\"GET\">\n\n  <label>\n     Search CSS-Tricks on Google: \n     <input type=\"search\" name=\"q\">\n   <\/label>\n\n  <input type=\"submit\" value=\"Go\">\n\n<\/form><\/code><\/pre>\n\n\n\n

When that form is submitted, we’ll intercept it and change the value to include the special syntax:<\/p>\n\n\n\n

var form = document.querySelector(\"form\");\n\nform.addEventListener(\"submit\", function (e) {\n  e.preventDefault();\n  var search = form.querySelector(\"input[type=search]\");\n  search.value = \"site:css-tricks.com \" + search.value;\n  form.submit();\n});\n<\/code><\/pre>\n\n\n\n

That’s all.<\/p>\n\n\n\n