views:

58

answers:

3

I am developing an HTML page that I want to convert in Search Engine.

I just have put a Textbox & Search Button. I am just allowed to use JavaScript. How do I convert it in Search Engine ?

A: 

You could use JavaScript to redirect the user to another search-engine. For instance you could simply take the user input from the text box and do a JavaScript redirect to http://www.google.com/search?q=$user_input where $user_input is the value of your form.

Writing a whole search-engine in JavaScript would simply be impossible.

Ham
Why impossible. You do not know the size of the searchable data.Here are a bunch of them http://www.google.com/search?q=search+engine+javascript
mplungjan
I suspected that someone would give me an example to prove me wrong. Sure, you're right. I assumed that writing a "big" search-engine is a task that should better not be done with JavaScript. Though some might argue differently.
Ham
how to fetch value of textbox from htmlpageliketextbox id ="user_input" but it gives google search result of http://www.google.com/search?q=$user_input as user_input but I want value of that text box
Sarang
@sarang: `var inputText = document.getElementById("user_input").value;`
Marcel Korpel
@Ham: yikes, there's a `blink` element on your home page. ;)
Marcel Korpel
@Marcel Korpel: Yes, just a quick and dirty one because I was bored. Somehow it just came over me. It felt so 90's :D
Ham
+4  A: 

You could achieve the same thing without using JavaScript by giving the form input name="q" and changing its action to http://google.com. Bringing JavaScript into the equation to do something so simple doesn't add any benefit to the user, and will lock out users without JavaScript from the functionality.

Mark Chorley
but remember to put the method to "GET" in that case. Elegant solution nevertheless ;)
Ham
And you should use `http://google.com/search` as action.
Marcel Korpel
A: 

You can use any search engine like google.com or bing.com to fetch the data. You can write your jquery script to send the input form data to any of the search engine and display the result in the same page.

<html>
  <head><title>search engine</title>
    <script type="text/javascript" src="jQuery.js"/>
    <script type="text/javascript" src="search.js"/>
  </head>

  <body>
   <div id="searchContainer">
     <form id="searchForm">
      <input id="searchbox" name="q" type="text"/>
      <button id="searchbutton" type="submit" value="search"/>
     </form>
   </div>
   <div id="result"></div>
  </body>
</html>

Following is the search.js file:

(function($){
  var googleUrl = "http://www.google.com?";

 function fetchAndInsertResults(inputText){
   $.ajax({ 
     url: googleUrl+inputText, 
     success: function(data){
               $('#result').html(data);
      }
    });
 }

  $('#searchbutton').bind('submit',function(){
    this.preventDefault();
    var inputText = $('#searchForm').serialize();
    fetchAndInsertResults(inputText);
  });
})(jQuery);
kapser
Oh please, don't use `#` as `action`; let your code degrade nicely when JS is unavailable. Also, don't use a `click` event on the button, a user can still press enter in the input field to submit the form (and in this case nothing will happen). Instead capture the `submit` event of the form. BTW, you forgot the parameter `inputText` in `fetchAndInsertResults`.
Marcel Korpel
hey marcel, thanks for the suggestions.
kapser
Won't this fail owing to Cross'Site Scripting restrictions? You could make it work with a server-side script to act as a proxy for connecting to Google, but that seems to be outside the remit of the question.
Mark Chorley
@Mark: I think you're right. @kapser: And I see that you don't produce a correct query string to serve to Google. Although you changed the event to `onsubmit`, you didn't change the ID to that of the form. And I didn't mean you should simply remove the `action` attribute, but let it point to `http://google.com/search`, as Mark suggested in his answer. But as he said, an XMLHttpRequest won't be possible anyway, because of XSS security restrictions.
Marcel Korpel