开发者

how to modify this javascript code to add a text box and receive input from user?

开发者 https://www.devze.com 2023-03-26 14:12 出处:网络
Here is a language translation code that google provides to detect the language in which the code is typed . This is the default code in which it translates the code from \"var text=\" field . I want

Here is a language translation code that google provides to detect the language in which the code is typed . This is the default code in which it translates the code from "var text=" field . I want to modify this code so as to recieve input from the user in a text box and on clicking the submit button it should display the result of language detected on the same page .

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
<title>Google AJAX Language API - Basic Translation</title> 
开发者_C百科<script type="text/javascript" src="//www.google.com/jsapi"></script> 
<script type="text/javascript"> 

google.load("language", "1");

function initialize() {
  var text = "¿Dónde está el baño?";
  google.language.detect(text, function(result) {
    if (!result.error) {
      var language = 'unknown';
      for (l in google.language.Languages) {
        if (google.language.Languages[l] == result.language) {
          language = l;
          break;
        }
      }
      var container = document.getElementById("detection");
      container.innerHTML = text + " is: <b>" + language + "</b>";
    }
  });
}
google.setOnLoadCallback(initialize);


</script> 
</head> 
 <body> 
<div id="detection"></div> 
 </body> 
</html>  


Try this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
<title>Google AJAX Language API - Basic Translation</title> 
<script type="text/javascript" src="//www.google.com/jsapi"></script> 
<script type="text/javascript"> 

google.load("language", "1");

function initialize(text) {
  google.language.detect(text, function(result) {
    if (!result.error) {
      var language = 'unknown';
      for (l in google.language.Languages) {
        if (google.language.Languages[l] == result.language) {
          language = l;
          break;
        }
      }
      var container = document.getElementById("detection");
      container.innerHTML = text + " is: <b>" + language + "</b>";
    }
  });
  return false;
}


</script> 
</head> 
 <body> 
<form onsubmit="return initialize(this.text1.value">
<input type="text" value="" name="text1" /><input type="submit" />
</form>
<div id="detection"></div> 
 </body> 
</html>  
0

精彩评论

暂无评论...
验证码 换一张
取 消