Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7779839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:44:54+00:00 2026-06-01T18:44:54+00:00

I am using the Microsoft Translation api (AJAX Version) from http://www.microsofttranslator.com/dev/ I am having

  • 0

I am using the Microsoft Translation api (AJAX Version) from http://www.microsofttranslator.com/dev/

I am having some issues with closures and callbacks, which I hope this code will demonstrate:

function translate(original){

    window.translateComplete = function(language) {

        if (language!="en"){
        alert(original +" "+language);
        }   
    }


    var windowsliveid = 'API_KEY_REMOVED';
    var el = document.createElement("script"); 
    el.src = 'http://api.microsofttranslator.com/V2/Ajax.svc/Detect'; 
    el.src += '?oncomplete=translateComplete'; 
    el.src += '&appId=' + windowsliveid; 
    el.src += '&text=' + escape (original); 
    document.getElementsByTagName('head')[0].appendChild (el);

}

translate("Au Revoir"); 
translate("Hola"); 

Now, my response comes back as

Hola es
Hola fr

The original variable is being overwritten, before the callback has had a chance to execute.

How do I avoid this so that it displays something like:

Hola es
Au revoir fr

I am trying to do this without using jQuery and when()

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:44:56+00:00Added an answer on June 1, 2026 at 6:44 pm

    Well, you are using the same callback function for every response, since window. translateComplete can only refer to one function. So every call to translate will overwrite window.translateComplete with a new function, which is a closure.

    You could create a new callback with a different name for each call. That’s what jQuery is doing.

    In it’s simplest form:

    var i = 0; // some running variable
    function translate(original){
    
        var cb_name = 'cb' + (i++); // create a new name
        window[cb_name]= function(language) {
            delete window[cb_name]; // remove function to not pollute the global scope
            if (language!="en"){
                alert(original +" "+language);
            }   
        }
    
        var windowsliveid = 'API_KEY_REMOVED';
        var el = document.createElement("script"); 
        el.src = 'http://api.microsofttranslator.com/V2/Ajax.svc/Detect'; 
        el.src += '?oncomplete=' + cb_name; // use the dynamic name instead
        el.src += '&appId=' + windowsliveid; 
        el.src += '&text=' + escape (original); 
        document.getElementsByTagName('head')[0].appendChild (el);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I
I need some code in my c# application to implement a transaction using Microsoft
I'm having some issues with spanish characters displaying in an iOS app. The code
I'm using Ajax in a HTML page to get a URL from a PHP
Using: Microsoft SQL Server 2008 Microsoft Visual Studio 2010 C# .NET 4.0 WinForms Ok
using Microsoft.SqlServer.Management.SqlStudio.Explorer; unable to find this pls assist [update] found the dll's in this
Using Microsoft Visual Studio 2010: Can I write this type of macro in C?
Using Microsoft SQL server 2008 I have a query that is in need of
When using Microsoft office automation I want to insert images into a spreadsheet. How
I'm using Microsoft Visual C# 2008 Express. In my main form, there's that X

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.