There is no way to make your second alert - the one where temp is undefined - to work because of the nature of the Language API. Specifically, the string to be translated is sent off to Google's servers, translated asynchronously, and returned. The asynchronous part is key. It means that your code continues executing even while the translation is being done. You will have to restructure your code so that anything which depends on the translation is executed within the callback function that you provide to the translate method (i.e., the function with the alert that works that is the third argument to the translate call).
Jeremy R. Geerdes Effective website design & development Des Moines, IA For more information or a project quote: http://jgeerdes.home.mchsi.com http://jgeerdes.blogspot.com http://jgeerdes.wordpress.com [email protected] Unless otherwise noted, any price quotes contained within this communication are given in US dollars. If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church! And check out my blog, Adventures in Web Development, at http://jgeerdes.blogspot.com ! On Mar 1, 2009, at 1:52 AM, ajaxLover wrote: > > In the below code, I want the translation to be stored in var temp! > but it never does. And I always have to end up using div / span tags > and filling them using container.innerHTML > > var temp; > var toTranslate = "Hello World!"; > google.language.translate(toTranslate, "en", "fr", function(result) > { > if (!result.error) > { > var container = document.getElementById("s1"); > container.innerHTML = result.translation; > temp = result.translation; > alert(temp); // here, temp has the translation, but this is called > after the below "alert" call > } > }); > alert(temp); // here, temp = undefined > > How do I get this to work? > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/Google-AJAX-Search-API?hl=en -~----------~----~----~----~------~----~------~--~---
