I am trying to write a translator box for my website. So far, I've been able to get working code from a tutorial:

<input id="ori" type="text" />
<button onclick="translate();">Translate</button>
<div id="trans"></div>

<script>
var languageFrom = "en";
var languageTo = "fr";

function translate() {
document.getElementById('trans').innerHTML="Translating... please wait";
var text= document.getElementById('ori').value;
window.mycallback = function(response) {
document.getElementById('trans').innerHTML=response;
}

var s = document.createElement("script");
s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=mycallback&appId=<MY-APP-ID>&from=" + languageFrom + "&to=" + languageTo + "&text=" + text;
document.getElementsByTagName("head")[0].appendChild(s);
}
</script>

(Script it from here: http://wizardsoweb.com/microsoftbing-translator-ajax-example/2011/05/?wpmp_switcher=desktop)

Basically, once I fill in my APPID, it will translate the text in the ori text box to French. Of course, this all works fine, but here's what I'm trying to do:

I want there to be two drop down menus. One will populate the languageFrom variable with a specified language, and another drop down menu which will populate the languageTo variable with a specified language.

I've already posted about this matter in a different post: Update script var's with value from drop down?. That way, when you select a value from the drop down menu, it populates the variable. I have a working example of this on jsFiddle:

http://jsfiddle.net/charlescarver/hk2bJ/1/

(I included my API key so it's easier to work on)

SO, I think the problem with this is that the script which controls the translation is loaded when the variables are empty, and then doesn't update when a drop down option is selected.

I think this can be fixed by calling the script when the button is clicked, instead of when the page is loaded. How can this be accomplished? The Microsoft support is poorly documented and I can't find a solution there or on google. Here's a link to the API documentation: http://msdn.microsoft.com/en-us/library/ff512385.aspx

有帮助吗?

解决方案

You're trying to attach event handlers to objects (select1 & select 2) that essentially don't exist yet (looking at your jsfiddle example) - which breaks the rest of your script, since you're actually failing to attach anything.

So you'll need to do something like this (not the greatest of conventions, but it demonstrates the point)

window.onload = function()
{
    document.getElementById("select1").onchange = function() {
        languageFrom = this.value;
    }
    document.getElementById("select2").onchange = function() {
        languageTo = this.value;
    }
}

(Which means you'll only attach the events once everything is loaded & available)

Something you can also do inline with your html elements, like seen below:

From: <select id="select1" onchange="languageFrom=this.value">
<option value="en">En</option>
<option value="fr">Fr</option>
</select>

To: <select id="select2" onchange="languageTo=this.value">
<option value="en">En</option>
<option value="fr">Fr</option>
</select>

Or Alternatively do everything inside your translate function (which means you don't need to attach anything to your dropdowns nor define any global variable)

function translate() {
    document.getElementById('trans').innerHTML="";
    var text= document.getElementById('ori').value;
    window.mycallback = function(response) {
    document.getElementById('trans').innerHTML=response;
    }

    var languageFrom = document.getElementById("select1").value;
    var languageTo = document.getElementById("select2").value;
    var s = document.createElement("script");
    s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=mycallback&appId=8B841CA7C1A03443682C52AD07B7775A7BD5B3AA&from=" + languageFrom + "&to=" + languageTo + "&text=" + text;
    document.getElementsByTagName("head")[0].appendChild(s);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top