Friday, January 14, 2005

Javascript lookup code

Here is the JavaScript code that does the Google and OneLook lookups.

It's been tested on IE only. Modification will probably be required for other browsers.


<script type = "text/javascript">


function doClick() {
openNewLookupWin('http://www.google.com/search?q=');
}


function doDblclick() {
openNewLookupWin('http://www.onelook.com/?w=');
}



function getSelection() {
if (navigator.appName == "Netscape") {
t = document.getSelection();
return t;
} else {
t = document.selection.createRange();
if (document.selection.type == 'Text' &&
t.text != '') {
return t.text;
}
return '';
}
}




function openNewLookupWin(call) {
var text = getSelection();
if (text == '') return;
window.open(call + '"' + escape(text) + '"');
}

</script>




Be sure to include the following in the <body> tag.
<body ondblclick = "doDblclick()" onclick = "doClick()">


1 comment:

Anonymous said...

That is one cool javascript!!