Click here to Skip to main content
15,894,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone.

I am working on a asp.net web site, where I want to implement search, although the searching part is done, I wanted to implement an added functionality to highlight a text and on right click get the text on the right click context menu and accordingly search which google has with its chrome browser and bing in IE.

I have made a customized right click menu and wish to implement the above in it.

Please help

Thank You

Sorry for the confusion, I guess I was not clear with my question,
I have linked an image of what I wish to implement in my web application [^].
I want to implement a database driven search within my web application (not Google search)

Thank you
Posted
Updated 18-Oct-14 20:55pm
v4

Google search API:
https://developers.google.com/custom-search[^],
https://developers.google.com/app-indexing[^].

Bing search API:
http://www.bing.com/developers/s/APIBasics.html[^],
http://www.bing.com/toolbox/bingsearchapi[^].

If your meant not to use such services, you probably though about your own search engine. You are welcome to develop your search technology, but you need to understand: search engines like Google and Binge have their index, a kind of "second internet" in the form of a huge volume of data collected from Internet and indexed, updated on regular basis. Does it sound realistic to you? :-)

—SA
 
Share this answer
 
v2
Comments
JPais 19-Oct-14 2:59am    
Sorry for the confusion, I have updated my question. I dont want to implement google search, I am doing a small database driven search and just wanted to add this functionality.
Sergey Alexandrovich Kryukov 19-Oct-14 12:37pm    
"Small database driven search", is, in contrast, something pretty trivial. You did not explain what could be your problem.
—SA
Afzaal Ahmad Zeeshan 19-Oct-14 4:29am    
search engines like Google and Binge have their index, a kind of "second internet" in the form of a huge volume of data collected from Internet and indexed, updated on regular basis. Exactly, Google updated their blog quite a while ago and said, they're having multiple Terabytes of data indexed, cached and saved inside their big databases (databases built on their own server, not any MySql or Ms Sql database) so that they can provide fast results.

Building own search engine is not a good idea. However, OP is trying to do the same, which would require Database and the same process they're doing.
If you have implemented the Context (Right click menu) options in your website. You can use the Selection of the Web API methods to get the selected text from the window.

https://developer.mozilla.org/en-US/docs/Web/API/Selection[^]

Then you can handle the click event on the menu item using jQuery or JavaScript and create a request to the Google servers alongwith the text from websites, like this

JavaScript
$('#menuItem').click(function () {
   // selectedText is the variable holding value from Selection
   window.open('http://www.google.com/search?q=' + selectedText);
});


For more on Window.Open read this document by MDN, https://developer.mozilla.org/en-US/docs/Web/API/Window.open[^]

Removing ambiguity

Ok, that case is really simple. Since you've already got the context, I am not going to rewrite the jQuery thing again. What you can do, is create a new page and name it "getResults.cshtml" (or the relative ASP.NET page format for your application, cshtml is a Web Pages page).

Once done, write this code to it,
C#
var wordToSearch = Request["word"];

//initialize the Database connection
// search for the relative content in it, 

// return the results (this will be passed down to the client)


How to pass the word to it? You can use the following jQuery code to pass the word to the next page, instead of Google server.

JavaScript
$('#contextMenu').click(function () {
   $.ajax({
     url: '/getResults',
     // selectedWord as explained in the above code
     data: {word: selectedWord},
     success: function(data) {
       // there was a successfull request, and data was returned
       alert('Response from the server was: ' + data);
     }
   });
});


This is the code, to send the data to the server, and get the response. All using an ajax request. If you need the code to search for the contents inside the Database, read this article of mine,

How to search for related query inside the Database using ASP.NET[^]
 
Share this answer
 
v2
Comments
JPais 19-Oct-14 3:01am    
Sorry for the confusion, I have updated my question. I don't want to implement Google search, I am doing a small database driven search and just wanted to add this functionality.

Also how to get the selected (or highlighted) text in the Context (Right click menu)
Afzaal Ahmad Zeeshan 19-Oct-14 3:10am    
Its ok! I have added a few more details to have your work done.

Read the MDN document on the Selection, you will find a better answer there.
JPais 19-Oct-14 4:21am    
Thank You Afzaal, I guess the problem can be resolved if I just get the selected Text in the context menu.
Can you help me how to get the selected text in context menu.
Afzaal Ahmad Zeeshan 19-Oct-14 4:25am    
window.getSelection() can be used to get the selection from the window. You can read more here, http://stackoverflow.com/questions/5643635/how-to-get-selected-html-text-with-javascript

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900