943,854 Members | Top Members by Rank

Ad:
Aug 14th, 2008
0

JAvascript Search Eninge Script

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
knarffrank is offline Offline
12 posts
since Aug 2008
Aug 14th, 2008
0

Re: JAvascript Search Eninge Script

Google for 'google search api'. You would also require the help of some server-side language along with Javascript to achieve this task.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Aug 15th, 2008
1

Re: JAvascript Search Eninge Script

~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.
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008
Aug 15th, 2008
0

Re: JAvascript Search Eninge Script

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.
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008
Aug 15th, 2008
0

Re: JAvascript Search Eninge Script

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.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Aug 15th, 2008
0

Re: JAvascript Search Eninge Script

Click to Expand / Collapse  Quote originally posted by MattEvans ...
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
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008
Aug 20th, 2008
0

Re: JAvascript Search Eninge Script

Reputation Points: 10
Solved Threads: 0
Newbie Poster
amithc is offline Offline
1 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: JDOMP problem
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Javascript: Open Url in Iframe using frameset





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC