The JavaScript webpage was supposed to load a map, but unfortunately, it couldn't load any content at all

Here is an HTML page with embedded JavaScript. The Map API Key is valid for my website domain, and the map can only load if I hide all the other JavaScript functions and keep the initialize() function as it is in Google's official example code. Otherwise, the page loads blank without displaying anything. Can anyone help me figure out why?

Thank you!

<html>   
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Real-time iphone tracker</title>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAzcrhlFUxvQMBI3qYkY87zhStzVKTwp4tXCAZpL7xj82pGpEoixSAvciDeL2RO_S1xLoGgxKBpbG6HQ&sensor=true"
        type="text/javascript"></script>
<script type="text/javascript">

var xmlobj;
function createXMLHttpRequest(){
   if(window.ActiveXObject)
   {
       return new ActiveXObject("Microsoft.XMLHTTP");
   }

   else if(window.XMLHttpRequest)
   {
       return = new XMLHttpRequest();
   }
}

function sendRequest(){

    xmlobj = createXMLHttpRequest();
    if(!xmlobj){
        alert('The browser is not compatible with XMLHttpRequest');
        return 0;
    }
    var indexurl = "index.php";
    xmlobj.onreadystatechange = stateHandler;
    xmlobj.open("GET", indexurl, true);
    xmlobj.send(null);

}
function stateHandler(){
  if(xmlobj.readyState == 4 && xmlobj.status == 200){
    document.getElementById("map_data").innerHTML= xmlobj.responseText;
  }
}

function initialize() {
  if (GBrowserIsCompatible()) {
    sendRequest();
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(22.37664, 114.184902), 13);
    map.setUIToDefault();

    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    var lngSpan = northEast.lng() - southWest.lng();
    var latSpan = northEast.lat() - southWest.lat();
    for (var i = 0; i < 10; i++) {
      var point = new GLatLng(southWest.lat() + latSpan * Math.random(),
                              southWest.lng() + lngSpan * Math.random());
      map.addOverlay(new GMarker(point));
    }
  }
}
</script>
</head>
<body onload="initialize()" >
<div id="map_canvas" style="width: 500px; height: 300px"></div>
<br>
<div> id="map_data" ></div>

</body>
</html>

Answer №1

A JavaScript bug was discovered in your code. The correct format for the createXMLHttpRequest function is as follows:

function createXMLHttpRequest(){
   if(window.ActiveXObject)
   {
       return new ActiveXObject("Microsoft.XMLHTTP");
   }

   else if(window.XMLHttpRequest)
   {
       return new XMLHttpRequest();
   }
}

It should be noted that you originally had written:

return = new XMLHttpRequest();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Applying the jqtransform plugin to all forms except for one

We've implemented the jQuery jqtransform plugin to enhance the appearance of our website's forms. However, there is one particular form that we'd like to exclude from this transformation. I made adjustments to the script that applies jqtrans ...

Is there a way to execute a Django For Loop within an HTML Div using JavaScript triggered by a Click Event?

I have a unique HTML div structured like this, <div class="coment-area "> <ul class="we-comet"> {% for j in comment %} <div id="delete_comment{{j.id}}" clas ...

Copy the value of a selected textbox to another using JQuery Auto Complete

I'm currently facing an issue where I need to assign one textbox's selected value to another textbox. Upon checking in the Firefox Browser Console, I've realized that the state variable (c_search.php?state) is not holding any value. Could s ...

How can I dynamically assign the formControlName to an <input> element within Angular?

I'm currently developing a component with the goal of streamlining and standardizing the appearance and functionality of our forms. The code snippet below illustrates the structure: Sample Implementation ... <my-form-input labelKey = "email" cont ...

Error: The directive template is unable to evaluate the given expression

I recently dove into AngularJS and hit a roadblock. Excuse the coffeescript and haml mix. # some-js-file.js.coffee app.directive "sr", -> restrict: "C" replace: true transclude: true scope: serviceRequest: "@servReq" template: "<div> ...

Identify a single characteristic of an object array

I need an Angular list that displays only the instances of a specific property from an array of objects, such as countries. $scope.testSites = [ { "site": "Testsite1", "country": "Country1", "customer": "Customer1"}, { "sit ...

The Right-Click Functionality in SlickGrid

Incorporating SlickGrid into my web application, I am currently contemplating whether to display the context menu based on the specific data row that is right-clicked. However, I am experiencing difficulty in triggering a right-click event as opposed to ...

Converting HLS streams with FFMPEG in ASP.NET Core 5.0 MVC

Overview I am currently developing a media streaming server using ASP.net Core REST Server, utilizing .NET 5.0 and ASP.net Core MVC. What I'm Looking For I require the ability to dynamically reduce the resolution of the original video file, such as ...

Storing an array in Firestore

I am currently struggling with using Firestore to store data on a webpage. I am attempting to store an array of custom objects, but for some reason Firestore is not allowing me to do so. I have attempted to pass it as an object of objects, but it keeps g ...

React with TypeScript: The children prop of this JSX tag is specifically looking for a single child of type ReactNode, but it seems that multiple children were passed instead

For my class project in APIs, I am using react-typescript but running into issues. My problem arises when attempting to loop through an array of "popular" movies using .map. However, I keep getting this error message: "This JSX tag's 'children&ap ...

What is the reason that the attr function fails to function in this particular scenario?

I want to implement a functionality where clicking on an input field clears its value, but I can't seem to make it work. Here is the HTML code: <input id='input' type="text" name="phppostvar" value="clear this"></input> This i ...

Learn how to extract substrings from a variable within an API using Vue.js

Exploring VueJs for the first time and looking to split a string by comma (,) retrieved from an API into separate variables. The data is coming from a JSON server. "featured-property": [ { "id": "1", " ...

Guide to designing a unique shape in JointJs by combining multiple basic shapes together

Is there a way to combine different shapes in Joint JS, such as creating a custom shape that includes both a rectangle and a circle? I am aware of the path method, but I'm not sure if it is suitable for creating combined shapes like this. ...

Converting to alphanumeric characters using JavaScript

Is there a way to efficiently encode a random string into an alphanumeric-only string in JavaScript / NodeJS while still being able to decode it back to the original input? Any suggestion on the best approach for this would be greatly appreciated! ...

How can I make the lines of my drawer thicker in Material UI?

Currently, I'm in the process of implementing a left-side navigation bar for a web application I'm developing. The main challenge I'm facing is customizing the styles as desired when using the Drawer component. Specifically, I'm aiming ...

My AJAX checkbox change event is not functioning, what could be causing this issue?

This is a sample of the code I'm working with: <form asp-action="Save" asp-controller="ClassesHeld" method="post"> <input asp-for="ClassHeldId" value="@Model.ClassHeldId" hidden /> <div class="form-group"> ...

I relocated the navigation HTML code to a new file and as a result, the JavaScript functionality is

After successfully implementing the hamburger icon animation in my navbar using JavaScript, I decided to move the nav code to a separate file for better organization. However, the decision resulted in an error where clicking on the burger icon returned a m ...

Managing dynamically loaded scripts in Meteor.js

This poses an interesting query: is there a way to regulate which scripts a client is provided with? I've partitioned my code into dynamically loadable segments using the import('dynamically_loadable_file') method, but each time it's ca ...

What is the process of declaring internal class variables within class methods in JavaScript?

Here's a query related to React JS. Can I define internal class variables within a method and then use them in other methods? For instance: class Something extends React.Component { state = { value: 'doesnt matter' }; somethin ...

Showcasing a separate website within an iframe

My iframe is not displaying the other website on my webpage. Can anyone suggest an alternative method using either iframe, div or simple JavaScript to achieve this? Any help would be greatly appreciated. I have attempted using an iframe but it doesn' ...