Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically loading Google Earth Javascript code

Status
Not open for further replies.

jtbob74

Programmer
Nov 20, 2009
2
US
Hi everyone,

I got assigned this little project when our web developer left, and I'm totally new to Javascript.

Basically we have Google Earth web pages that will show KML data for various project. Currently, they are all separate pages.

What I've been asked to do is create a template page that gets passed where the KML code resides. Which was easy enough, but the Javascript code for each page has sections that refer specifically to HTML objects.

So what I thought of doing was moving out the Google Earth Javascript code to an external file that resides with the KML code, that way the template can then load in dynamically the Javascript code for that KML data.

I found Javascript code online that will load in an external .js file, which it does.

<script src="
<script type="text/javascript">
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}

function jsinit()
{
var gedir = gup('KMLCode');
var jssource = " + gedir + "/geJavascript.js";
var fileref=document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", jssource);
document.getElementsByTagName("head")[0].appendChild(fileref);
}
</script>

</HEAD>
<body onload='jsinit()' id='body'>

I pass the variable in KMLCode of where the KML data and Javascript code reside, and the function JSInit will load the file in dynamically. The problem is now I get an error that Google Earth object isn't seen by the code in the external file.

Is this some type of scope issue, or is there some other issue I need to be away of when trying this type of code loading?

Thanks,
Robert
 
I get an error that Google Earth object isn't seen by the code in the external file.

What is the actual error? Do you have a URL that highlights the problem?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Hi Dan,

Here's the error I get:
Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; InfoPath.1; .NET CLR 3.0.30729)
Timestamp: Mon, 23 Nov 2009 19:38:27 UTC

Message: 'google.earth' is null or not an object
Line: 12
Char: 5
Code: 0
URI:
Here's the URL that I'm using to load the page:

The page parses the KMLCode parameter from the URL and forms the link to dynamically load in the external Javascript file at:

So from the top post, you can see it loads in the Google Earth object with the proper site key, but then the external code doesn't see the Google Earth object. If I place the external code back into the main document, everything works fine. Since I'm so new to Javascript, I don't know where to go next to resolve this issue.

Thanks,
Robert
 
I'm not quite sure what your page is doing, because visiting the URL you have very briefly shows a blue page and they clears it before redirecting off somewhere that never seems to load.

Unfortunately, I'm away for 2.5 months as of tomorrow morning, so I won't be able to take a look into it until Feb next year - so hopefully someone else will be able to do so (assuming they can get to the page and not get redirected away immediately, like I do).

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top