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.

jQuery(document).ready(function() {
	jQuery('p').bind('dblclick', function(e){
		window.location = '<?php bloginfo('home'); ?>/?s='+getSelectedText();
    });

});

function getSelectedText(){
    if(window.getSelection){
        return window.getSelection().toString();
    }
    else if(document.getSelection){
        return document.getSelection();
    }
    else if(document.selection){
        return document.selection.createRange().text;
    }
}

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 :

dfn {
 	color: blue;
	text-decoration: underline;
	font-size: 14px;
}

…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.

jQuery(document).ready(function() {
	
	jQuery('dfn').bind('click', function(e){
		window.location = jQuery(this).attr('title');
    });
});

…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 *