Как сделать поисковик на сайте html
Перейти к содержимому

Как сделать поисковик на сайте html

  • автор:

Как создать поле поиска

Поля поиска распространены на каждой веб-странице. Они очень полезны при поиске конкретной информации. Поле поиска может иметь функцию ввода с автодополнением, что помогает пользователям при поиске.

В этой статье мы пошагово покажем, как можно создать поле поиска с помощью CSS и HTML.

  1. Создайте HTML
  • Создайте тег <form>, который используется для добавления HTML форм к веб-странице для пользовательского ввода.
  • Добавьте тег <input> внутри элемента <form>.
  • Добавьте также placeholder с выражением «Search here!» и класс «Search».
  • Добавьте другой input c классом «submit» и значение «search». ска, используя только CSS и HTML.

Теперь у нас есть поле поиска, но необходимо добавить стиль.

  1. Добавьте CSS

Есть три элемента, к которым необходимо применить стиль: «form», «search» и»submit».

  • Добавьте фоновый цвет текстовой части (body).
  • Начните оформление классов «form», «search»и «submit».
  • Установите width, margin, padding, и font-size.

После того, как все свойства добавлены, запустите код и увидите результат!

Как сделать поисковик на сайте html

Every website needs a search bar through which a user can search the content of their concern on that page. A basic search bar can be made using HTML, CSS, and JavaScript only. Advance searching algorithms look for many things like related content and then shows the results. The one that we are going to make will look for substrings in a string.

Using HTML

In this section, we will write HTML part of the code. In HTML, we will just link our Stylesheets and our JavaScript file. Input tag is used for the creation of the search bar and it includes several attributes like type, placeholder, name. We also need a list of items which will hold different animal names that will allow us to search for animals through this. The classes and ID’s used in tags will be defined in stylesheet below.

Output:

Using CSS

Though the above input tag and the ordered list looks fine, it still needs some styling. For the search bar styling, some margin and padding are added to make it look clean. The measurements are in percentage so that it adjusts itself when used in any size of the screen (Mobile, Desktop etc). Webkit transition is used to change the width of the Search bar when clicked. The initial width of search bar is 30%, but when it is clicked, it will change to 70% with an ease-in ease-out transition of 0.15 seconds.

Output
After adding Styling, our page should look like this.

Note: If the styling of your page doesn’t change, make sure the style.css file is in the same folder as index.html.
It is still incomplete, as we still need the JavaScript to complete the functionality of this search bar.

Using JavaScript

In the HTML code of search bar, we gave the input an and onkeyup we called, the function “search_animal”. onkeyup calls the function every time a key is released on the keyboard.

We first get our input using getElementById. Make sure to convert it to lower case to avoid case sensitivity while searching. An array of documents is stored in x. This contains every list that has After that a loop is run to check if innerHTML of every document includes the input substring if it doesn’t, the display property is set to ‘None’ so that it is invisible on the front end.

How can I add a Google search box to my website?

I am trying to add a Google search box to my own website. I would like it to search Google itself, not my site. There was some code I had that use to work, but no longer does:

When I try making a search, it just directs to the Google homepage. Well, actually it directs here: https://www.google.com/webhp

Does anyone have a different solution? What am I doing wrong?

5 Answers 5

Sorry for replying on an older question, but I would like to clarify the last question.

You use a «get» method for your form. When the name of your input-field is «g», it will make a URL like this:

But when you search with google, you notice the following URL:

Google uses the «q» Querystring variable as it’s search-query. Therefor, renaming your field from «g» to «q» solved the problem.

Cryothic's user avatar

This is one of the way to add google site search to websites:

Figured it out, folks! for the NAME of the text box, you have to use «q». I had «g» just for my own personal preferences. But apparently it has to be «q».

Anyone know why?

(The reason your code isn’t working is because the GET request name is now "q" instead of "g".

I recommend using one of the two methods below:

Method 1: Simply send a GET request directly to Google (Best and most simple option)

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *