views:

300

answers:

2

Hi there,

I'm trying to make use of google's ajax apis in a chorme extension's "content script". On a regular html page, I would just do this:

<script src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
<script>
  google.load("language", "1");
</script> 

But since I'm trying to load the tranlation library dynamically from js code, I've tried:

script = document.createElement("script");  
script.src = "http://www.google.com/jsapi";  
script.type = "text/javascript";  
document.getElementsByTagName("head")[0].appendChild(script); 
google.load('language','1')

but the last line throws the following error:

Uncaught TypeError: Object # has no method 'load'

Funny enough, when i enter the same "google.load('language','1')" in chrome's js console, it works as intended...

I've also tried with jquery's .getScript() but the same problem persists...

Does anybody have any clue what might be the problem and how it could be solved?

Many thanks in advance!

Martin

+1  A: 

I have got this working like this:

<script type="text/javascript">
    var headID = document.getElementsByTagName("head")[0];
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = 'http://www.google.com/jsapi';
    headID.appendChild(newScript);
</script>
<script type="text/javascript">
    google.load("language", "1");
</script>

It returned no errors.

XGreen
A: 

Are you sure? That doesn't work for me.

joshholat
This should be a comment, not an answer.
bzlm