Safari/Webkit executing function prematurely following ajax:complete event

I am currently working on an AJAX call which is functioning properly. However, I am facing an issue regarding the dimensions of an image. Since the images are dynamically resized, I cannot declare their dimensions in HTML. When I try to fetch these dimensions by calling a function during the completion state, Webkit browsers return 0 as the dimensions while Firefox provides the correct values.

$.ajax({
        url: event.path,
        cache: false,
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            handler(XMLHttpRequest.responseText);
        },
        success: function(data, textStatus, XMLHttpRequest) {
            handler(data);
        },
        complete: function() {
            textBoxWidth();
        }
    });

The following code snippet shows the function being called:

function textBoxWidth() {
    $("#project .textBox").css("width", $("#project .imgBox:last img").width());
}

I have attempted using the condition:

if (jQuery.browser.safari && document.readyState != 'complete')

But it seems that the document state remains unchanged even after the DOM has been fully loaded.

Any suggestions or advice would be greatly appreciated.

Answer №1

Here's another demonstration of how you can make jQuery wait while loading using the `jQuery.active` variable.

This variable outputs 1 if jQuery is still in the process of loading, and 0 once it has finished loading.

$.ajax({
        url: path,
        cache: false,
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            handler(XMLHttpRequest.responseText);
        },
        success: function(data, textStatus, XMLHttpRequest) {
            handler(data);
        },
        complete: function() {
            if (jQuery.active !== 0) {
                setTimeout( arguments.callee, 100 );
                return;
            }
            //fancyFunction();
        }
    });

Answer №2

After tinkering with some code, I found a solution that works for me.

$.ajax({
        url: event.path,
        cache: false,
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            handler(XMLHttpRequest.responseText);
        },
        success: function(data, textStatus, XMLHttpRequest) {
            handler(data);
        },
        complete: function() {
            var imgCount = $("#project .content").find("img");
            if (imgCount.length > 0) {
                if ($("#project .content img:last").width() == 0) {
                    setTimeout( arguments.callee, 100 );
                    return;
                }
            //fancyFunction();
            }
        }
    });

Here's what I did: I checked for images in my target div. If there are images present and the width of the last image is zero, I rechecked the width after 100 ms. Otherwise, I proceeded with other actions.

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

After establishing the connection between Ajax and PHP with a relationship table, the inserted data is not being displayed

I am new to using Ajax and Php and have a question about inserting data into a Mysql table using Bootstrap Modal, Ajax, and Php. Currently, I have a table named "tbl_employee" and a page called "index.php." In the "index.php" page, there is a Bootstrap m ...

The request to remove the product from the server at URL http://localhost:8000/product/[object%20Object] encountered an internal server

I'm having trouble setting up the AJAX delete method. I keep getting a 500 internal server error and despite following tutorials, I remain frustrated. Can someone please help me correct my code? This is using Laravel 5.8 and jQuery 3.3. <section ...

"Exploring the best way to loop through multiple JSON data in a jQuery response

https://i.stack.imgur.com/O0F6g.pngMy JSON response contains multiple data points presented as follows. WellNames […] 0 {…} id 56 well_name AL HALL api 34005205550000 1 {…} id 498 well_name BONTRAGER api 34005233850000 2 {…} id 499 ...

Steps for Implementing an Event Listener in JavaScript

While working on a page in Chrome, I encountered an issue where I wanted a modal to show up when a user clicked on an image. However, the functionality was not working as expected on my localhost. Upon further inspection, I believe there might be a problem ...

Tips for transferring information from one php page to another php page via ajax

I am attempting to retrieve data from one PHP page and transfer it to another page through the use of Ajax. JavaScript : $.ajax({ url: "action.php", success: function(data){ $.ajax({ url: "data.php?id=data" ...

The AppJS warning: A potentially dangerous JavaScript attempt to access a frame with a URL

I am currently working on a desktop application where I am trying to filter specific divs inside an iframe using AppJS. However, I am facing an issue with hiding some of the div elements. Here is the code snippet: <!DOCTYPE html> <html> <s ...

Get the JS file by tapping the download button, and access the

In creating the web page, I utilize a modular approach. Leveraging node js and the node-static server are essential components of this process. One specific requirement I have is implementing file downloads from a computer to a device using a button trigg ...

How to use JQuery UI sortable to automatically scroll to the bottom of the page

Having trouble with a few sortable tables and here is how I initialized the sortable object: var options = { helper: customHelper, handle: ".moveTargetDeliverables", containment: "#fieldset_deliverables_summary", tolerance: 'pointer&a ...

Using jQuery and Bootstrap in an ASP.NET Core project

I am encountering an issue with the configuration of bootstrap and jquery within my project, causing these tools to fail to load properly. The problem seems to be that bootstrap is loading before jquery, resulting in error messages appearing when I check ...

Remove Chosen Pictures (Checkbox/PHP/MySQL)

I am currently displaying images from a Database using the following HTML code: <li> <input type="checkbox" id="1" /> <a rel="gallery_group" href="images/big/1.jpg" title="Image 1"> <img src="images/small/1.jpg" alt="" ...

Parsing Problem---Is there a Parsing Error?

My code includes a function that calculates the total of cells and then displays it in a textbox: function UpdateTotal() { var total = parseFloat('0.0'); $("#<%= gvParts.ClientID %>").find("tr").not(".tblResultsHeader").each(funct ...

The attempt to fetch the submitted data via the PHP URL was unsuccessful

I have a form and I've created a URL to fetch data. The data is being fetched properly, but when I try to access the URL, it shows {"error":"null"}. How can I retrieve the submitted value? I am having trouble displaying the web services as I attempt t ...

Guide on Highcharts Bar Chart: Adjusting the Minimum Bar Width/Length

We are currently experiencing some challenges within our application where bars are not displaying if a particular value is significantly smaller compared to the largest value. To better illustrate this issue, you can refer to the following jsFiddle: http ...

Ways to handle the ajax callback content

I'm dealing with an AJAX code that looks like this: var req = new XMLHttpRequest();<br> req.open('GET', 'http://www.example.org/', false); req.send(null);<br> if(req.status == 200)<br> var response = http_att ...

Using jQuery's sortable functionality to rearrange rows in a table can cause conflicts with Angular's orderBy feature

In the past, my angular app used a table with orderBy to sort rows based on a specific column. By clicking on a table header, the orderBy arguments would change and the list would be sorted according to the values in that column. Now, I am experimenting w ...

Transforming the input button into images

I'm new to JavaScript and I'm looking to change the show button and hide button to images instead. The show button image should be different from the hide button image. Can anyone guide me on how to achieve this? Click here for reference $( ...

"PHP and MySQL Join Forces: Troubleshooting UNION Query Issues

I am encountering an issue with debugging this query using PHP/MySQL-AJAX. The variable $param is the result of an AJAX call on a form textbox. Essentially, I am attempting to create a dynamic search across three database tables that have different fields, ...

Several jquery libraries are experiencing malfunctions

<head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $(".slidingDiv").hide(); $(".show_hide").show().click ...

Switch between showing and hiding a div by clicking on the panel header and changing the symbol from + to

I need assistance with a panel feature on my website. The panel should expand when the "+" symbol is clicked, displaying the panel body, and the "+" symbol should change to "-" indicating it can be collapsed by clicking it again. There is a slight twist t ...

Retrieve all elements from an array using jQuery

How do I extract all the elements from the array outside of the function? $.each(Basepath.Templates, function(i){ templateArray = new Array({title: Basepath.Templates[i].Template.name, src: 'view/'+Basepath.Templates[i].Template.id, descri ...