2011-03-02 122 views
3

谷歌翻譯,有一些書籤,讓與1次點擊翻譯,例如:什麼是[對象窗口]?

javascript:var t=((window.getSelection&&window.getSelection())||(document.getSelection&&document.getSelection())||(document.selection&&document.selection.createRange&&document.selection.createRange().text));var e=(document.charset||document.characterSet);if(t!=''){location.href='http://translate.google.com/?text='+t+'&hl=en&langpair=auto|en&tbb=1&ie='+e;}else{location.href='http://translate.google.com/translate?u='+encodeURIComponent(location.href)+'&hl=en&langpair=auto|en&tbb=1&ie='+e;}; 

這段JavaScript代碼,打開當前網頁(目標= _self)的翻譯頁面,但我想它會打開一個新的窗口(標籤)進行翻譯。所以改爲:

javascript:var t=((window.getSelection&&window.getSelection())||(document.getSelection&&document.getSelection())||(document.selection&&document.selection.createRange&&document.selection.createRange().text));var e=(document.charset||document.characterSet);if(t!=''){window.open('http://translate.google.com/?text='+t+'&hl=en&langpair=auto|en&tbb=1&ie='+e);}else{window.open('http://translate.google.com/translate?u='+encodeURIComponent(location.href)+'&hl=en&langpair=auto|en&tbb=1&ie='+e);}; 

我的問題就在這裏:當我運行的代碼,它打開了翻譯一個新的窗口,並做到這一點;但非英文頁面內容被替換爲[object Window],但我不想更改原始頁面內容...

我該怎麼辦?

謝謝。

回答

8

末添加void(0),所以也就沒有價值。如果最後一個表達式有一個值(在這種情況下是一個窗口),則頁面被替換。

+0

它的工作原理,謝謝 – gcivan 2014-01-14 08:38:59