InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Site Performance tutorials

  • Chrome PageSpeed insights
  • Google custom search with lazy loading
  • Install and monitor memcache for php on Ubuntu
  • Internal vs external css
  • Javascript in header vs footer
  • PHP apc - setup and performance benchmarks
  • Php apc vs memcache
  • Using inline image for site performance
 
  • Home
  • > Tutorials
  • > Web Development
  • > Site Performance

How to use Google custom search with lazy loading approach

By admin | Last updated on Mar 20, 2016

If you have Google adsense account then you can use Google custom site search for your website. I’m using it on a wordpress site but this article is relevant to any site. Here is the code you get from Adsense account for site custom search box:

<form action="http://infoheap.com/search/" id="cse-search-box">
<div>
  <input type="hidden" name="cx" value="partner-pub-xxxxxxxxxxxxxxxx:xxxxxxxxxx" />
  <input type="hidden" name="cof" value="FORID:10" />
  <input type="hidden" name="ie" value="UTF-8" />
  <input type="text" name="q" size="20" />
</div>
</form>
<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"></script>

This loads a javascript and a watermark image from Google. These days most people search for content on Google. Even when users click on a result and come to a web site, they often don’t search on that web site. They prefer to go back to Google and search. So custom site search is not really a highly used feature.

Moreover with mobile device being popular, saving even one javascript is good for user experience. Here is another approach you can use where you place some search image or button on site and when user clicks on it, laod the javascript. This will lead to one extra click for some users but faster site load for most users.

Here are the steps you can use:

  1. Replace search box with a search image. e.g.
    <div id="searchdiv"><img width=24 height=24 src="/img/search_icon-24.png"></div>

    Here is how it looks:
    google-cse-placeholder-img-before-click

  2. Place this inline javascript somewhere in the end of the html document. Preferably just before </body> tag. Placing javascript in the end of the document is always a good idea and should be done whenever possible. You may want to make changes to the code in case you are using different div names.
    var searchcontent = '<form action="http://infoheap.com/search/" id="cse-search-box">'
        + '<div>'
        +   '<input type="hidden" name="cx" value="partner-pub-xxxxxxxxxxxxxxxx:xxxxxxxxxx" />'
        +   '<input type="hidden" name="cof" value="FORID:10" />'
        +   '<input type="hidden" name="ie" value="UTF-8" />'
        +   '<input type="text" name="q" size="20" />'
        + '</div>'
        + '</form>';
    function ih_search_prepare () {
      jQuery('#searchdiv img').click(function(){
        jQuery('#searchdiv').remove('img');
        jQuery('#searchdiv').html(searchcontent);
        jQuery.getScript("http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en")
      });
    }
    jQuery(document).ready(function() {ih_search_prepare();});
    

    This will attach a click handler to the search image. The click handler will embed html Google custom site search box code in searchdiv and also fetch the javascript needed for custom site search.

    Here is how it looks when clicked:
    google-cse-after-clickNow user can type the query and do search using Google custom site search.

Suggested posts:

  1. Online tools to create comic strips
  2. HP 2515 printer review (deskjet ink advantage all-in-one)
  3. Chrome extension tutorial – hello world
  4. Traceroute outcome from Bangalore to AWS Virginia and California
  5. How to use w3 total cache for wordpress
  6. How to delete files starting with dash/hyphen
  7. Ssh automation on Amazon EC2 Ubuntu Linux
  8. Verify a web site in google search console (webmaster tools)
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged jQuery, Page Speed, Site Performance, Tutorials, Web Development, Wordpress, Wordpress Performance
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress