JAvascript Search Eninge Script

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Aug 2008
Posts: 12
Reputation: knarffrank is an unknown quantity at this point 
Solved Threads: 0
knarffrank knarffrank is offline Offline
Newbie Poster

JAvascript Search Eninge Script

 
0
  #1
Aug 14th, 2008
I want to create a search engine page where i can get google's number of result

for example: When I type the word "Car" it will search in the google's search engine and it will return the number of results.. Note: the page will not go to google's page
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: JAvascript Search Eninge Script

 
0
  #2
Aug 14th, 2008
Google for 'google search api'. You would also require the help of some server-side language along with Javascript to achieve this task.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: JAvascript Search Eninge Script

 
1
  #3
Aug 15th, 2008
~s.o.s~ is, again, completely correct ... and you do need some server side language since JavaScript has what is known as a sandbox which keeps it from doing cross-site scripting, as a security model.

The following is an unorthodox approach and i would recommend following ~s.o.s~'s advice and research the official API.

But all the server-side script you need is this (PHP) -- so long as Google doesn't change it's layout format.

googit.php
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2. // Results <b>1</b> - <b>10</b> of about <b>1,420,000,000</b> -- CURRNET GOOGLE FORMAT
  3. $query = $_GET['q'];
  4. $result = file_get_contents( "http://www.google.com/search?q=$query" );
  5. $found = preg_match( '#Results <b>\d+</b> - <b>\d+</b> of about <b>([\d,]+)</b>#', $result, $match );
  6. if ( $found ) {
  7. $count = str_replace( ',', '', $match[1] );
  8. } else {
  9. $count = "'false'";
  10. }
  11. print "google( $count );";
  12. ?>

And to avoid the complexities of Ajax and iFrames this is the JavaScript to do the rest
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <style type="text/css">
  5. body {
  6. padding: 60px;
  7. }
  8. </style>
  9. <script type="text/javascript">
  10. window.onload = function () {
  11. document.getElementById('googit').onchange = function () {
  12. if ( script = document.getElementById('script') ) {
  13. script.parentNode.removeChild( script );
  14. }
  15. var head = document.getElementsByTagName('head').item(0);
  16. var script = document.createElement('script');
  17. script.setAttribute( 'id', 'script' );
  18. script.setAttribute( 'type', 'text/javascript' );
  19. script.setAttribute( 'src', 'googit.php?q=' + this.value );
  20. head.insertBefore( script, head.firstChild );
  21.  
  22. document.getElementById('output').value = '... please wait ...';
  23. };
  24. };
  25.  
  26. function google ( result ) {
  27. var output = document.getElementById('output');
  28. if ( result == 'false' ) {
  29. output.value = 'INVALID SEARCH';
  30. } else {
  31. output.value = result;
  32. }
  33. }
  34.  
  35. </script>
  36. </head>
  37. <body>
  38. <label for="googit">Get Google Count</label><br />
  39. <label for="googit">Input: </label><input id="googit" type="text" />
  40. <input type="submit" value="Touch Me" /><br /><br />
  41. <label for="output">Result: </label><input id="output" type="text" />
  42. </body>
  43. </html>

Please forgive how lacking in style the above page is ... ;-)
I only worked this up for personal interest as a proof of concept

Ciao
Last edited by langsor; Aug 15th, 2008 at 12:23 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: JAvascript Search Eninge Script

 
0
  #4
Aug 15th, 2008
Okay, my proof of concept fails for multiple word searches ... :-(

But I believe these two changes fix that
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // replace javascript line (IE wants this)
  2. script.setAttribute( 'src', 'googit.php?q=' + this.value.replace( / /, '+' ) );
  3.  
  4. // add PHP line (FF wants this)
  5. $query = $_GET['q'];
  6. $query = str_replace( ' ', '+', $query );

I'm done now ... thanks for bearing with me.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: JAvascript Search Eninge Script

 
0
  #5
Aug 15th, 2008
Google don't take kindly to people running automated queries ( including scrapes ) without using their "official APIs", so you may find they block your server from communicating with the Google site if it makes alot of requests like this.

Which is hypocritic to say the least, since that's exactly how Google gathers its own information.

But anyway, certainly good stuff for a proof of concept.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: JAvascript Search Eninge Script

 
0
  #6
Aug 15th, 2008
Originally Posted by MattEvans View Post
Google don't take kindly to people running automated queries ( including scrapes ) without using their "official APIs", so you may find they block your server from communicating with the Google site if it makes alot of requests like this.

Which is hypocritic to say the least, since that's exactly how Google gathers its own information.

But anyway, certainly good stuff for a proof of concept.
Good reminder Matt -- google is strict.

Anyone trying to do SEO and trying to keep up on google's rules will make your head spin with all the restrictions they impose (admittedly, most are for the good of all though)

Cheers
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1
Reputation: amithc is an unknown quantity at this point 
Solved Threads: 0
amithc amithc is offline Offline
Newbie Poster

Re: JAvascript Search Eninge Script

 
0
  #7
Aug 20th, 2008
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC