Calculation of image size in JQuery fails to function properly on Chrome and Safari browsers

CONCEPT

This page features a combination of visible and hidden div elements, with the hidden ones becoming visible upon clicking a specific span element. One of these hidden elements contains multiple images with overlaid text that are randomly positioned on the page with a 20px margin from each edge.

HTML (includes jQuery 1.8.3, jQuery UI 1.10.1, and jQuery UI touch-punch):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
  <body>

    <div class="pattern" style="background-image: url('m.png');"></div>

    <div class="shadow pat-main-block">
      <span class="mood-board">Show container</span>
    </div>

    <!-- Container div -->
    <div id="mood-board">

      <span class="hide-mood shadow">Hide container</span>

      <div id="img1" class="drag mood-img">
        <img id="mimg1" class="shadow" src="mood/brick-mood-1.png">
        <span class="mood-name shadow">text</span>
      </div>

      [...]
      
    </div>
    <!-- End of the container div -->

 </body>

. . .

The aim is to create a dynamic template that can accommodate various images and quantities while ensuring they are not positioned too close to the edges of the screen. Despite some issues in Chrome and Safari regarding image sizes and calculations, the overall functionality works well when tested in Firefox.

Additionally, there is a desire to optimize the JavaScript code to make it more generic, eliminating the need to specify individual elements within the script.

Different outcomes between website and JSfiddle: http://jsfiddle.net/n3q92/1/ This disparity poses a challenge, especially since disabling certain parts does not impact the functionality as expected.

UPDATE: While the code performs adequately in Chrome, Safari presents compatibility issues.

UPDATE 2: Notably, Firefox exhibits distinct behavior concerning image width and height retrieval compared to Chrome and Safari, particularly when loading fresh pages without cache. The discrepancy warrants further exploration into why this occurs despite the images being theoretically loaded behind the scenes.

Answer №1

Instead of using $(document).ready(), the resolution was to implement $(window).load(). Surprisingly, the JavaScript code functioned correctly on JSfiddle in Chrome.

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

Dynamically removing buttons with JQuery

I am trying to dynamically add buttons to a page based on an array, but I'm running into a problem. Whenever I add a new name to the array, it prints out all of the buttons instead of just the one I added. I need help figuring out how to erase all the ...

Locate the next div element using its class name and check if it is the last element in the sequence

I am attempting to iterate through a sequence of <div> elements by using the has() method to check if the element contains a specific class. Then, I aim to navigate to the next element with the help of the next() function. Instead of utilizing hasCla ...

"Headers cannot be set once they have been sent to the client... Error handling for unhandled promise rejection

Having trouble with cookies in the header - specifically, encountering an error at line number 30. The error message reads: "Cannot set headers after they are sent to the client." Additionally, there is an UnhandledPromiseRejectionWarning related to a prom ...

Failing to retrieve data from Ajax response

When handling requests in a servlet, the following code snippet processes the request received: Gson gson = new Gson(); JsonObject myObj = new JsonObject(); LoginBean loginInfo = getInfo(userId,userPwd); JsonElement loginObj = gson.toJsonTree(loginInfo) ...

Learn the process of copying and pasting a file in PhoneGap with the help of JavaScript

I am currently working on a mobile application using the phoneGap framework. I have placed my existing .db file in the www directory of phoneGap. I need to copy this file from the www directory to the internal memory of the application at /data/data/applic ...

Using Laravel and AJAX to save multiple selected filters for future use

I've been struggling with this issue for a few weeks now and just can't seem to wrap my head around it. On my webpage, I've implemented four filters: a search filter, a year filter, a launch-site filter, and a country filter. Each of these ...

Is it possible to showcase CSS and JavaScript code exactly as it appears when the files are saved in their respective formats, but embedded within an HTML

Recently, I've been working with notepad++ and have come across an interesting challenge. In my index.html file, I have embedded css and javascript code. Is there a way to display this code as it appears when saved in their respective files, but maint ...

Determining the selected option's value made by the user

On my index.php file, which contains the form and function, I have the following code: $(function() { $.ajax({ type: "POST", url: "invtype.php", data: "getrevenuetype=true", success: function(b){ $("#revenue ...

React-Native - Issue with TypeError: attempting to call a function that is undefined (specifically when using the 'object.map' method)

I've tested everything from the itemList to the reducer and action, it's working fine and successfully deleting the desired item. However, I encountered an error afterwards. I'm not sure where I went wrong. Can someone guide me on what steps ...

Utilize Vue.js to incorporate an external JavaScript file into your project

Currently, I am utilizing Vue.js 2.0 and facing an issue with referencing an external JavaScript file in my project. The index.html file contains the following script: <script type='text/javascript' src='https://d1bxh8uas1mnw7.cloudfro ...

Exploring the methods of accessing $scope within an AngularJS directive

My custom directive is designed for handling form controls like input, select, and radio buttons. Each input has a default value set, and if the corresponding value exists in $scope.answers, it should be displayed in the input box. The code snippet below ...

The functionality of returning false on ajax response does not effectively prevent the form from submitting

I'm encountering an issue where the return false statement doesn't seem to work when using an AJAX call. The form is still getting submitted successfully despite trying to prevent it with a conditional check on the response from the AJAX request. ...

Issue encountered while attempting to implement custom fonts through CSS

When attempting to implement a custom font on my website using the CSS file, I am having trouble getting it to display correctly. Instead of seeing the desired font, only the default font is showing up. Can someone please review the CSS code below and le ...

Angular's input event fails to trigger for dynamically generated fields when pasted into them

When working with a form that has dynamically generated input fields, I encountered an issue. I needed to display a preview of the input contents on a div while pasting content into the fields. This project is built using Angular 11. Below is my .ts file: ...

What is the best way to establish a default selection in Angular?

After retrieving JSON data from the server, it looks something like this: $scope.StateList = {"States": [ { "Id": 1, "Code": "AL", "Name": "Alabama" }, { "Id": 2, "Code": "AK", "Name": "Alask ...

Unable to retrieve the saved user from the Express.js session

Below is the code in question: app.post('/api/command', function (req, res, next) { var clientCommand = req.body.command; console.log("ClientCommand: ", clientCommand); if (!req.session.step || req.session.step === EMAIL) { ...

Utilizing AJAX requests to execute create, update, and delete actions all from one streamlined file instead of separating them into three distinct

In my JavaScript code, I am currently making XMLHttpRequests to separate PHP files for different functionalities in my app - like add.php, update.php, and delete.php. However, having three separate files feels repetitive. Is there a way to consolidate al ...

What is the best method for retrieving all input fields within a form that are of the types text, password, file, textarea, and selectbox?

Is there a way to retrieve all input fields of specific types within a form using jQuery? jQuery("#signup-form").find('input').each(function(index, element) { }); I am aware that I can target text boxes with: jQuery("#signup-form").find(&apos ...

My jQuery function is failing to execute properly following an AJAX callback

I am currently utilizing asp.net 3.5 on my project. Everything operates smoothly with a jquery function triggered by the 'hover' event. An issue arises after refreshing the browser, which triggers some divs to update using ajax calls. After th ...

What is the best way to eliminate the blue color from a Boostrap 5.3 accordion item when it is opened?

As shown in these two images, I am looking to remove the blue color and blue border when opened: https://i.sstatic.net/vOIna.png https://i.sstatic.net/7eLfK.png <div class="accordion-item"> ...