JavaScript experts, help pls

Soulcatcher

New member
I need to implement basiv google maps functionality in my web app. As you may know, to use google maps i need a key which i get from google. So by design requirements this key is stored in web_info.xml file (i'm using tomcat).

I dont want to serve whole html from the servlet, i want to serve a static html and then use XMLhttprequest to implement everything else.

So the problem is how to use the key. To use google maps i need to import their JavaScript code by adding this script element in header:
<script src="http://maps.google.com/maps?file=api&v=2&key=abcdefg" type="text/javascript"></script>
where "abcdefg" is a key.

So the problem is that i dont know the key in the beginning - i have to get this key from the servlet. Suppose i have a servlet Key that just does this when it's called:
String url = " var googleurl = \"http://maps.google.com/maps?file=api&v=2&key="+key+"\"";
out.print(url);
where key is String variable which i get from web_info.xml some time before in the Key servlet.

So now instead of adding the original <script> element into the header, i add this
<script type="text/javascript" src="/app/servlet/Key"></script>
This initializes variable googleurl after this tag is processed, so i can use it later.

Question :) - how can i use it? How can i use variable for the src attribute? For example, this doesnt work:
<script src=googleurl type="text/javascript"></script>
I tried various quotes around googleurl - it just doesnt work. When i'm trying to call finctions from google script Firefox just says that this function isnt defined - ie google script wasnt imported.

I tried to do this instead in window.onload function:
var header = document.getElementsByTagName("head")[0];
var s = document.createElement("script");
s.setAttribute("src", googleurl);
s.setAttribute("type", "text/javascript");
header.appendChild(s);
This kinda works - after i do this firefox is trying to get some data from maps.google.com indefinitely - so while it's a progrees ;), it still doesnt work.

Any help is appreciated :)
 
Back
Top