<script src="http://www.google.com/jsapi?key=YOURKEY" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1');
</script>
Code language: JavaScript (javascript)
<input type="text" title="Real Time Search" name="searchbox"/>
<input type="button" id="searchbtn" value="Search" onclick="search(searchbox.value)"/>
<div class="data" id="web-content"></div>
Code language: HTML, XML (xml)
When user will write a query and push Search button, a request will be made to Google Search using Custom Search API and the results are fetched. These results are then copied into the DIV. google.search.WebSearch
. 2. Set a callback function that will get call once the results for the search are fetched. 3. Call the execute()
method with search query as argument. 4. In callback function, iterate through the results and copy it to container DIV. var webSearch;
webSearch = new google.search.WebSearch();
webSearch.setSearchCompleteCallback(this, webSearchComplete, [webSearch]);
function webSearchComplete (searcher, searchNum) {
var contentDiv = document.getElementById('web-content');
contentDiv.innerHTML = '';
var results = searcher.results;
var newResultsDiv = document.createElement('div');
newResultsDiv.id = 'web-content';
for (var i = 0; i < results.length; i++) {
var result = results[i];
var resultHTML = '<div>';
resultHTML += '<a href="' + result.unescapedUrl + '" target="_blank"><b>' +
result.titleNoFormatting + '</b></a><br/>' +
result.content +
'<div/>';
newResultsDiv.innerHTML += resultHTML;
}
contentDiv.appendChild(newResultsDiv);
}
function search(query) {
webSearch.execute(query);
}
Code language: JavaScript (javascript)
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Viral Proto looks amazing and quite efficient... .. Congrates...
jQuery is in use, you can still optimize and compact your code by utilizing it.
@dharmesh, Thanks for the comment
@vikas, yes sir.. I am using jQuery but I dont know why. I may became lazy to do document.getElementById() instead of $('#'). Yes I think there is still scope left in this example. :)
I liked this one. Keep it up.
Thanks Nilesh :)
Also, Let me know if you have some ideas on which I can write blogs. I always have shortage of things to write upon (Although there are millions of things in tech world).
hello sir your code is not working. why
Hi.. .You can write on face recognition in Java, PHP.
hi,
thank you very much for your modified google ajax codes. i like it. please send me more modified codes in my mail address.
and yah....................since from 3 months i am trying my best to crack the google ajax search limit.............but still the result is same. i.e: 8 pages with 8 display is the maximum. so could you crack the result limit? send me the code in my mail...............tarsicherink@yahoo.com
hi Viral, can you write on "finding a location and its surronding 50 km places from its ZIP/PIN using google API"? thanx in advance.
Is it search only in my local webserver hosted in my office? Please guide me to do so.
Unfortunately, the code here does not return the same results as the google search engine. Try, for example, for the search string LTC1403. The results are very different.