Friday, June 15, 2007

Keyword search trick

Searching your Blogs made easy...

A two function javascript that allows your visitors to query the web with any keywords from your page.

The first function checks if the item you double clicked on the document is a text then it passes it to the second function that appends the text value to your specified query string.

You can opt for the result to be displayed on the same page by using:

document.getElementById("<$id$>").innerHtml = ""

Or Pop up a new window:

window.open("<$variables$>")

Or redirect the page:

this.document.location=("" + "")

Code Snippet:

  1. function searchmySite() {
  2. if (navigator.appName!='Microsoft Internet Explorer') {
  3. var mykeywords = document.getSelection();
  4. omg(mykeywords);
  5. }
  6. else {
  7. var mykeywords = document.selection.createRange();
  8. if(document.selection.type == 'Text' && mykeywords .text>'') {
  9. document.selection.empty();
  10. omg(mykeywords.text);}
  11. }
  12. function omg(mykeywords ) {
  13. var searchStr = "<Put your search string here>"
  14. while (mykeywords.substr(mykeywords.length-1,1)==' ')
  15. mykeywords=mykeywords.substr(0,mykeywords.length-1)
  16. while (mykeywords.substr(0,1)==' ')
  17. mykeywords=mykeywords.substr(1)
  18. if (mykeywords) document.location=(searchStr + mykeywords);
  19. }
  20. }
  21. document.ondblclick=searchmySite


If at first you fail, call it version 1.0

No comments: