JavaScript - search box functionality - Asked By bipin pathak on 20-Jun-12 11:01 AM

hi all


i have a website which is built in html. It contain a search textbox in its top and i want that the search fuctionality should work on it ie whatever the word from website material like tab name or keywordsi enter into it and press the submit button it should redirect to the page which contain that word or sentence.
And the word or sentence to be inserted is from website only .



as ths is very important for me kindly reply me an appropriate answer

thanks
Mir Ishaq replied to bipin pathak on 20-Jun-12 11:23 AM
Hi Bipin

You shouldn´t use client javascript to access databases for several reasons (bad practice, security issues, etc) but if you really want to do this, here is an example:


var connection = new ActiveXObject("ADODB.Connection") ;

var connectionstring="Data Source=<server>;Initial Catalog=<catalog>;User ID=<user>;Password=<password>;Provider=SQLOLEDB";

connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");

var searchvalue = document.getelementbyid('Yourtextboxname')

rs.Open("SELECT * FROM table", connection);
rs.MoveFirst
while(!rs.eof)
{
   document.write(rs.fields(1));
   rs.movenext;
}

rs.close;
connection.close;


you can modify the above program segment according to ur need.

Regards
Ishaq
Mir Ishaq replied to bipin pathak on 20-Jun-12 11:23 AM
Hi Bipin

You shouldn´t use client javascript to access databases for several reasons (bad practice, security issues, etc) but if you really want to do this, here is an example:


var connection = new ActiveXObject("ADODB.Connection") ;

var connectionstring="Data Source=<server>;Initial Catalog=<catalog>;User ID=<user>;Password=<password>;Provider=SQLOLEDB";

connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");

rs.Open("SELECT * FROM table", connection);
rs.MoveFirst
while(!rs.eof)
{
   document.write(rs.fields(1));
   rs.movenext;
}

rs.close;
connection.close;


you can modify the above program segment according to ur need.

Regards
Ishaq
Mir Ishaq replied to bipin pathak on 20-Jun-12 11:23 AM
Hi Bipin

You shouldn´t use client javascript to access databases for several reasons (bad practice, security issues, etc) but if you really want to do this, here is an example:


var connection = new ActiveXObject("ADODB.Connection") ;

var connectionstring="Data Source=<server>;Initial Catalog=<catalog>;User ID=<user>;Password=<password>;Provider=SQLOLEDB";

connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");

rs.Open("SELECT * FROM table", connection);
rs.MoveFirst
while(!rs.eof)
{
   document.write(rs.fields(1));
   rs.movenext;
}

rs.close;
connection.close;


you can modify the above program segment according to ur need.

Regards
Ishaq
bipin pathak replied to Mir Ishaq on 20-Jun-12 11:30 AM
thanks ishaq
it was a bit helpful.
[)ia6l0 iii replied to bipin pathak on 20-Jun-12 12:08 PM
The best solution to leverage, according to me, is to implement Google Site Search.  If you already have your site live on the internet, you can test this feature in the below said url. You can get a live preview of search on the site you mention. 

Please visit the following link for a quick tour, and resources.
http://www.google.com/enterprise/search/products_gss.html 

And also note that you for sure have certain tricks that can let you search any website using search engines. For e.g. in google, "site:eggheadcafe.com robbe" searches eggheadcafe for the word robbe.Obviously, your website should be indexed and uptodate by the search engine. 

Also you would be interested to know, that there are age-old Javascript search engines like JSSIndex, that can index a certain collection of webpages, and then let you search as well. Search about "jssindex" and you shall find more info on that. 

Hope this helps.
Jitendra Faye replied to bipin pathak on 21-Jun-12 12:36 AM
Try this code , which will help you to search particular string  in web page.

Reference from-

http://www.javascripter.net/faq/searchin.htm


solution-


var TRange=null;

function findString (str) {
 if (parseInt(navigator.appVersion)<4) return;
 var strFound;
 if (window.find) {

  // CODE FOR BROWSERS THAT SUPPORT window.find

  strFound=self.find(str);
  if (!strFound) {
   strFound=self.find(str,0,1);
   while (self.find(str,0,1)) continue;
  }
 }
 else if (navigator.appName.indexOf("Microsoft")!=-1) {

  // EXPLORER-SPECIFIC CODE

  if (TRange!=null) {
   TRange.collapse(false);
   strFound=TRange.findText(str);
   if (strFound) TRange.select();
  }
  if (TRange==null || strFound==0) {
   TRange=self.document.body.createTextRange();
   strFound=TRange.findText(str);
   if (strFound) TRange.select();
  }
 }
 else if (navigator.appName=="Opera") {
  alert ("Opera browsers not supported, sorry...")
  return;
 }
 if (!strFound) alert ("String '"+str+"' not found!")
 return;
}