Friday, February 15, 2008

Translate selection text to chinese in new window

Firefox:
javascript:var dict=function(){var select=document.getSelection();var url='http://sh.dict.cn/search/?q='+select;window.open(url);return ;};dict();

闭包(closures)的写法:(function(){var q=String(window.getSelection());var url='http://sh.dict.cn/search/?q='+q;window.open(url);return;})();

IE7:
javascript:var dict=function(){var select=document.selection.createRange().text;var url='http://sh.dict.cn/search/?q='+select;window.open(url);return ;};dict();

上面function里最后面必须是return; (相当于是return undefined;)否则当前页面会被function return的结果重写,要防止结果重写,也可以将dict()方法调用包含到void操作符内:
void((function(){var q=String(window.getSelection());var url='http://sh.dict.cn/search/?q='+q;window.open(url);return false;})());
usage:
1. 新建一个书签,目标URL用上面的代码,IE会有个提醒,需要确认一下。
2. 在打开的网页里,如GOOGLE READER,要查某个词,选中此词。
3. 点击刚新建的书签(bookmarklet),IE会再次提醒。

No comments :