faking links & dblclick search

Being a noob at jquery, I was very satisfied with these two : the first is a one liner to search the blog when someone doubleclicks on a word, the second is an easy fake anchor.

double click search

If you double click (in some browsers?) you select the word under the cursor. Bind the double click event of a paragraph element to a function that uses the standard worpdress search, using bloginfo(‘home’) to get the home url, with the selected text as ?s= search parameter, and relocates to the search result page.

  1. jQuery(document).ready(function() {
  2.  jQuery('p').bind('dblclick', function(e){
  3.   window.location = '<?php bloginfo('home'); ?>/?s='+getSelectedText();
  4.     });
  5.  
  6. });
  7.  
  8. function getSelectedText(){
  9.     if(window.getSelection){
  10.         return window.getSelection().toString();
  11.     }
  12.     else if(document.getSelection){
  13.         return document.getSelection();
  14.     }
  15.     else if(document.selection){
  16.         return document.selection.createRange().text;
  17.     }
  18. }

fake a link

You can also fake a hyperlink, makes seo life easier.

Any surplus inline element will do, in this case the dfn tag, as most have the title attribute that I use to store the url  :

<dfn title="http://elgoog.rb-hosting.de/index.cgi?dir=/Top/News/&amp;page=Satire/">hyperlink</dfn>

Style the dfn element with a bit of css :

  1. dfn {
  2.   color: blue;
  3.  textdecoration: underline;
  4.  fontsize: 14px;
  5. }

…and it looks like a hyperlink, add some jQuery to bind a click event on the dfn element to a function that jumps to the title attribute.

  1. jQuery(document).ready(function() {
  2.  
  3.  jQuery('dfn').bind('click', function(e){
  4.   window.location = jQuery(this).attr('title');
  5.     });
  6. });

…and we have the link that search engines do not index as a link, but it does work.

 

Posted in wordpress and tagged , .

Leave a Reply

Your email address will not be published. Required fields are marked *