Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Need help getting Javascript to open in a new window

    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
    Last edited by Dukeswe; 12-14-2010 at 03:43 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    <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.

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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: http://www.Mysite.com/target%20=_bla...ypZ==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.....
    Last edited by Dukeswe; 12-14-2010 at 05:24 PM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Philip goofed. That's not even close to right.

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

    You have to convert that location.href into
    Code:
    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).

    SO...
    Code:
    <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>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    <FORM>
    <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;};"
    </FORM>

    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

    /Dukeswe

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well I have no clue either what it does 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.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    So *can* you show this on your (non-working) page? Live? So I can see if there is indeed a usable error message showing?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ahhh...I may have found the bug!

    On this line:
    Code:
          ? 'http://translate.google.com/?text='+t+'&hl=sv&langpair=auto|sv&tbb=1&ie='+e;
    *REMOVE* the semicolon at the end!!!! Should be just
    Code:
          ? '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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It works now Thanks alot for your help. Seems like that semicolon was the culprit all along.

    Thanks yet again for helping me out

    /Dukeswe


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •