Prompt function of Google music search bar php Correction Code

Source: Internet
Author: User

Problem description

When loading the page, quickly move the cursor to the search bar. After the page is loaded, a search prompt is displayed after the search bar is initialized. at this time, any input content will be part of the Search Prompt, not the search keyword. as follows:

Cause

The JavaScript in the search bar is initialized and executed on onload. because there are many page image requests, it takes about 3 seconds to load the page completely, and the tabindex in the search bar is set to 1, the search-first user can easily encounter it.

The following is the JS Code that I decomcompiled based on my own understanding. The page will execute the m. hint. initHint method during onload to add the prompt function to the search box.
Copy codeThe Code is as follows:
/**
* Add a prompt for the search box
* @ Param searchTip prompt information
* @ Param searchBoxId: Search the input box ID
* @ Param hideBoxId the keyword hides the box ID.
*/
M. hint. initHint = function (searchTip, searchBoxId, hideBoxId ){
Var searchBox = document. getElementById (searchBoxId );
Var hideBox = null;
If (searchBox ){
If (hideBoxId ){
HideBox = document. getElementById (hideBoxId );
}
L. events. listen (searchBox, "blur", l. bind (m. hint. onInputBlur, null, searchBox, hideBox), false );
L. events. listen (searchBox, "focus", l. bind (m. hint. onInputFocus, null, searchBox, hideBox), false );
If (hideBox ){
L. events. listen (searchBox, "change", bind (m. hint. onInputChange, null, searchBox, hideBox), false );
HideBox. value = m. hint. getInputValue (searchBox );
}

// Here, assign the search prompt to the Temporary Variable
M. hint. Gh [searchBox] = searchTip;
// If the search box exists, add a temporary variable and a gray class to the search box
M. hint. onInputBlur (searchBox );
}
};

M. hint. onInputBlur = function (searchBox, hideBox ){
M. hint. fi (searchBox );
HideBox & m. hint. onInputChange (searchBox, hideBox );
};

M. hint. fi = function (searchBox ){
If (searchBox ){
Var searchTip = m. hint. Gh [searchBox];
If (searchTip & (searchBox. value. trim () = "" | searchBox. value = searchTip )){
SearchBox. setAttribute (m. hint. IS_HINT, "1 ");
SearchBox. className + = "hint ";
SearchBox. value = searchTip;
}
}
};

Solution
Shorten the time between DOM node loading and JavaScript initialization in the search box. you can execute JS immediately after loading the search box. Google does not need SEO for its own products. of course, it is best to execute it in DOM ready.

Extended knowledge
I remember writing an article about how to add text prompts In the WordPress search box. my solution is simple and crude. I only use the keyword in the box to determine whether it is a keyword or a prompt message. (So my prompt message is very long)

During the analysis of this case, I found that Google has a good solution and can be used for reference. it stores Real search information by hiding the input box, so it can distinguish between full keywords and prompt text.

Remarks
This small problem has been bothering me for a long time (almost every time I encountered it). Today I can't help but read the code. I hope Google engineers can see this article to quickly solve it. (I have provided all the suggestions for modification. If you do not change the suggestions, you can leave it alone)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.