The JavaScript window variable is not being returned from an AJAX request without a delay

At the beginning of my script, I set a default value for the name of a kmz file that I need for Google Maps. This value is stored in a window variable.

window.kmz="Delaware_River_Basin.kmz"

After running an ajax request, I receive the correct value for the kmz file, which gets stored in the window kmz variable. The script is working fine and returning the expected result.

$.ajax({
    type: 'POST',
    dataType: 'json',
    url: '/includes/kmz.php',
    data: {id: window.whichID},
    success: function(data) {
        window.kmz = data[0];
    },

});

Later on in the code, I need to use this kmz file for the maps.


var watersheds = new google.maps.KmlLayer({
    url: 'http://' + window.location.hostname + '/map/' + window.kmz + '?a=2',
    suppressInfoWindows: true
});
watersheds.setMap(map);

In Mozilla, the window.kmz variable always receives the value from the ajax call. However, in Explorer and Chrome, the default value is retained instead of the value from the ajax call. It seems like there is a delay in updating the variable for Explorer and Chrome. Interestingly, if I add an alert box to display window.kmz before defining the watersheds variable, it shows the default value first and then updates correctly with the ajax value. Why does Mozilla handle this differently compared to Explorer and Chrome?

Answer №1

For every piece of code that requires the presence of the kmz element, it must be placed within the success callback. Alternatively, if you have control over the server, you can establish cookies on the server side to retrieve the kmz element synchronously.

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

What is the best way to control the class name of elements using jQuery in an MVC4 application?

Among the <li> elements, one of them contains the class "current". Determining which <li> is the current one. View below: @{ int k = 10; //the value changes with each request } <ul class="car"> @foreach (var item in modelCoun ...

What is the process for utilizing AngularJS's multiple $http calls to retrieve data from a single PHP file?

I'm currently experimenting with multiple AngularJS ajax calls to a single php file in order to retrieve different json data based on the request. Below is the code snippet I am working with: var myApp = angular.module('myApp', []); myApp ...

Trouble displaying Django Ajax values in drop-down menu despite successful retrieval

I have implemented a search feature using Ajax to display blog titles in a drop-down suggestion list. Despite not encountering any errors and being able to view the data populated through inspect element > network > response in the browser, the temp ...

Tips for identifying HTML tags that include a specific attribute automatically

Is there a way to automatically identify an HTML tag that contains a specific attribute like data-wow-delay? This query revolves around wow.js. The goal is to have the classes wow bounce added automatically to any HTML tag that has this particular attribu ...

At what point does a dynamically loaded CSS file in the head section of a webpage actually

If the answer is already out there somewhere, I would really appreciate a link because I just can't seem to find it. When loading .php pages, I either include 'header.php' directly with <?php include 'header.php'; ?> or on ...

Displaying an ejs file on the client's side

Struggling to implement AJAX functionality for cloning an 'item' template. I am attempting to insert an EJS template containing information about the newly cloned item into the main EJS page after each POST request. However, I am having difficult ...

What is the best way to utilize a variable across all functions and conditions within an asynchronous function in Node.js?

I need to access the values of k and error both inside and outside a function. Initially, I set k to 0 and error to an empty string, but unexpectedly, the console prints out "executed". var k = 0; var error = ""; const { teamname, event_name, inputcou ...

Detecting when the Ctrl key is pressed while the mouse is hovering over an element in React

In my project, I have implemented a simple Grid that allows users to drag and drop items. The functionality I am trying to achieve is detecting when the mouse is positioned on the draggable icon and the user presses the Ctrl key. When this happens, I want ...

React JS Bootstrap Dropdown Troubleshooting

I'm facing a challenge in my React.js project while trying to implement a filter. The issue is that the list appears when I click on the button, but it doesn't disappear on the second click or if I click outside the list. Initially, I imported t ...

Encoding a string in JSON that contains the "#" symbol along with other special characters

The client side javascript code I have is as follows: <html> <script type="text/javascript" src="js/jquery.min.js"></script> <script> $(document).ready(function() { //var parameters = "a=" + JSON.stringify( ...

Certain javascript files have had illegal characters mistakenly added to them

There seems to be an issue with the js files or server on certain pages, as they are not loading in Firefox: with firefox: SyntaxError: illegal character 癡爠浥湵楤猠㵛≶敲瑩捡汭敮產崻 ⽅湴敲⁩搨猩映啌敮畳Ⱐ獥灡牡瑥 ...

What is the best way to transfer my object along with its unique textures from Blender to ThreeJS?

I've been using the STL Loader in three.js to import objects from Blender, but I'm facing an issue where the textures are not appearing. I suspect it has something to do with the MeshBasicMaterial of the object. I attempted to switch it to Phong ...

Is there a way to retrieve two separate route details using jQuery simultaneously?

Clicking the checkbox should display the Full Name: input type="text" id="demonum" size="05"> <button type="button" onclick="load_doc()">click</button><br><br> <input type="checkbox" id ="check" > The r ...

javascript The array is showing up as blank

I am trying to return the entire object where linkable is set to true. Even though I am pushing the objects, the this.releaseDescriptionDatesArray remains empty. It seems like my logic is correct, so not sure why the array is not populating. for (const it ...

Having trouble getting Firestore/Firebase to work with Next.js?

Hey everyone, I could really use some help here. I've been working on integrating Firebase into my Next.js app for the API. Everything works well when I build and run locally, but once I deploy to Vercel for production, I encounter a 500 - Internal S ...

Maximizing the Potential of SSJS ContinueRequest

Is there a way to incorporate ContinueRequest into the script shown below in order to bypass the 2500 limit? <script runat="server"> Platform.Load("Core","1"); try { var DEkey = Request.GetQueryStringParameter(&qu ...

Is there a way to insert json data into a form input field using jQuery?

I am attempting to insert the JSON result into an input as the value. This is the code I am using: $.ajax({ type:"POST", url: '{% url "url_searchTour"%}', data: data1, success: function(jsonAjaxResult){ console.log(J ...

How is it possible for this variable to be altered without any modifications made to it in the current function?

This particular function receives two arrays as input: arrOne, which is an array comprising arrays of numbers, and arrTwo, which is an array containing numbers. My goal here is to generate every possible combination, followed by obtaining the unique combin ...

Issue with loading Babel preset in a monorepo setup

I'm working with a monorepo setup using Lerna and it's structured as follows: monorepo |-- server |-- package1 |-- package2 All packages in the repo make use of Babel. After installing all 3 projects, yarn copied all the required @babe ...

Make the adjustment from an H1 tag to an H2 tag with the help of

I need assistance with changing the HTML code from using an <h1> tag to a <h3> tag, using Vanilla JavaScript. Here is the code snippet in question: <h1 class="price-heading ult-responsive cust-headformat" data-ultimate-target=" ...