External Loading of Api

3 views
Skip to first unread message

bobby

unread,
Sep 25, 2008, 6:57:05 AM9/25/08
to Google AJAX API
Hi,

Please see the below html page:

{
<html>
<head> Test Page
<script type="text/javascript" src="http://www.google.com/jsapi">
</script>
<script src="script.js" type="text/javascript">
</script>
<script type="text/javascript">
google.load("language", "1");
google.setOnLoadCallback(function(result)
{
alert("Load Completed");
});
</script>
</head>
<body>
<div id="test">
The Input Text is: Hello
<br/>
<button type="submit" onClick="onSubmit();">Submit</button>
</div>
<div id="translation">
</div>
</body>
</html>
}


Below is the JavaScript File:

{
function onSubmit()
{
alert("on Submit");
google.language.translate("hello", "en" , "es", function(result)
{
alert("in handle result");
if (!result.error)
{
var translated = document.getElementById("translation");
translated.innerHTML = "<p>The translated text is:</p><br/
>"+result.translation;
}
});
}
}

The above works perfectly fine. However, I dont want to perform the
load separately in the html file. I want to put all js code in the
separate script file and hence calls to "google.load()" api also I
want to invoke from external file.

In the above if I make the following changes:

{
<script type="text/javascript">
function onStartup()
{
google.load("language", "1");
google.setOnLoadCallback(function(result)
{
alert("Load Completed");
});
}
</script>

and make the following change:

<body onload="onStartup();">

}

The page will not work.

My Question is Is there an alternate way to Load the js language api
externally? If not can it be provided in the near future?

Thanks and regards,
Bobby

Jeremy R. Geerdes

unread,
Sep 25, 2008, 7:02:08 AM9/25/08
to Google-AJAX...@googlegroups.com
The major problem with your experimental code that I see is that you
will never call the alert('"Load Completed"); bit because the onload
event has already fired by the time you call
google.setOnLoadCallback. You could try using this:

google.load('language','1',{callback:function(){alert('Language API
load completed.');}});

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
jgee...@mchsi.com

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!

Jeremy R. Geerdes

unread,
Sep 25, 2008, 9:26:03 AM9/25/08
to Google-AJAX...@googlegroups.com
Of course, the alternative would be to change your Javascript code to
something like this:

<script type="text/javascript">
google.load('language','1');
google.setOnLoadCallback(function(result){
alert('Load Completed');
});

Rev. Jeremy R. Geerdes, Pastor
Debra Heights Wesleyan Church
4025 Lower Beaver Rd.
Des Moines, IA 50310

p/f: (515) 279-5212
h: (515) 277-7491
jgee...@mchsi.com

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
jgee...@mchsi.com

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!

Reply all
Reply to author
Forward
0 new messages