View Full Version : Need help getting Javascript to open in a new window

12-14-2010, 04:40 PM
Hi, I am using a plugin called "HTML- javascript adder" on Wordpress which allows me to put a javascript code in my sidebar of my site. The javascript code I have translates whatever you have marked on the website to swedish using google translate.

The problem I have is that I don't know how to get it to open the translation in a new window since i am not very familiar with javascript.

Anyone know how to update this code to make it open in an new browser window?

<A HREF="javascript:if(typeof yzxypZ==typeof alert)yzxypZ();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=sv&langpair=auto|sv&tbb=1&ie='+e;}else{location.href='http://translate.google.com/translate?u='+encodeURIComponent(location.href)+'&hl=sv&langpair=auto|sv&tbb=1&ie='+e;};
" >Klicka här för att översätta det du markerat till svenska </A>

Any help is greatly appreciated :)

Philip M
12-14-2010, 05:38 PM
<A HREF="target = _blank; javascript:if(typeof yzxypZ==typeof alert)........

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

12-14-2010, 06:21 PM
I just tried what you wrote but it didn't work. When i clicked on the link it didn't open in a new window and it instead tried to find this page which obviously doesn't exist: www.Mysite.com/target%20=_blank;%20javascript:iftypeof%20yzxypZ==typeof%20 and so on.

Any other way or did I do something wrong?

This is how it looks now: <A HREF="target = _blank; javascript:if(typeof yzxypZ.....

Old Pedant
12-14-2010, 11:25 PM
Philip goofed. That's not even close to right.

You can't do
location.href="xxx" because that will always change the *current* window's contents.

You have to convert that location.href into

window.open( "xxx","_blank","width=800,height=400,...");

where the "xxx" is whatever you *were* assigning to location.href. You can either use "_blank" to force a new window very time or you could use any name of your own choosing (e.g., "TRANSLATE") to re-use the same popup. The third argument there allows you to specify the various characteristics of the popup window, and it is optional (if not given, the popup will default to the same characteristics as the current window...which also means it will open as a new tab, not a new window, in many browsers).


<A HREF="javascript:if(typeof yzxypZ==typeof alert)yzxypZ();
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);
var url = (t!='')
? 'http://translate.google.com/?text='+t+'&hl=sv&langpair=auto|sv&tbb=1&ie='+e;
: 'http://translate.google.com/translate?u='+encodeURIComponent(location.href)+'&hl=sv&langpair=auto|sv&tbb=1&ie='+e;
window.open( url, 'TRANSLATE', '...optional window characteristics...' );
"> Klicka här för att översätta det du markerat till svenska </A>

12-15-2010, 01:41 AM
Thanks for trying to help me. I copy pasted exacly what you had written and it didn't work :/
Now nothing happens when you click on the text to get the translation done.

I have also added a button instead of having text to click on and it works great when opening in the same window.

This is the latest working code that opens in the same window:

<INPUT type="button" value="Maskinöversätt markerad text" onClick="javascript:if(typeof yzxypZ==typeof alert)yzxypZ();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=sv&langpair=auto|sv&tbb=1&ie='+e;}else{location.href='http://translate.google.com/translate?u='+encodeURIComponent(location.href)+'&hl=sv&langpair=auto|sv&tbb=1&ie='+e;};"

I don't know what went wrong with the previous fix you did as I don't know much javascript at all and just copy paste the codes.

But if you want to give it another try I appreciate every effort you do to help :)


Old Pedant
12-15-2010, 02:03 AM
I could well have made a typing error or missed a character.

And I certainly don't know what yzxypZ( ) is supposed to do.

It's hard to debug this without seeing the whole page in action.

12-15-2010, 12:40 PM
Well I have no clue either what it does :p I could show you where I found the code though.
http://translate.google.com/translate_tools And on the bottom of the side you see buttons with different languages on them. And if you hover over them you can see the javascript code. So i simply took the javascript from one of them so if you want to see it in action you can go there.

Old Pedant
12-15-2010, 09:55 PM
So *can* you show this on your (non-working) page? Live? So I can see if there is indeed a usable error message showing?

12-15-2010, 10:03 PM
The site is still under construction so you need a password to reach it. I could pm you with the password if you want so you can see for yourself.

At the moment I am using the code with the button that works but it opens on the same page.

Do you want me to change to the code you made for me so you can see if there is any error message?

Old Pedant
12-15-2010, 10:50 PM
Ahhh...I may have found the bug!

On this line:

? 'http://translate.google.com/?text='+t+'&hl=sv&langpair=auto|sv&tbb=1&ie='+e;

*REMOVE* the semicolon at the end!!!! Should be just

? 'http://translate.google.com/?text='+t+'&hl=sv&langpair=auto|sv&tbb=1&ie='+e

Yes, install that on your page and if it doesn't work PM me.

12-15-2010, 11:03 PM
It works now :D Thanks alot for your help. Seems like that semicolon was the culprit all along.

Thanks yet again for helping me out :)