JS seems to kick in only after a couple of page refreshes

I'm facing an issue with my 3 columns that should have equal heights. I've implemented some JavaScript to achieve this, which you can see in action through the DEMO link below.

<script>
  $(document).foundation();
</script> 
<script>
   if(!(/iPhone|iPad|iPod|Android|webOS|BlackBerry|Opera Mini|IEMobile/i.test(navigator.userAgent) )) {
jQuery(document).ready(function($){
    var inHeight = $("#wrapper").innerHeight();
    $("#wrapper .col").each(function(){
        $(this).height(inHeight+"px");
        $(this).find('.content').height((inHeight-60)+"px");
    });
}); 
}
</script>

The problem is that the column height adjustment only works when I refresh the page, and sometimes it takes multiple refreshes for it to take effect. Is there a way to resolve this issue? Thank you!

This is how my JavaScript code is structured:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script src="js/modernizr.js"></script>
<script src="js/foundation.min.js"></script> 
<script>
  $(document).foundation();
</script> 
<script>
   if(!(/iPhone|iPad|iPod|Android|webOS|BlackBerry|Opera Mini|IEMobile/i.test(navigator.userAgent) )) {
jQuery(document).ready(function($){
    var inHeight = $("#wrapper").innerHeight();
    $("#wrapper .col").each(function(){
        $(this).height(inHeight+"px");
        $(this).find('.content').height((inHeight-60)+"px");
    });
}); 
}
</script> 

Answer №1

If you're looking for IE8+ support, let's tackle your issue using only HTML, no JavaScript needed!! :)

check out the demo here

Cleanse your code of all JS and float, then refer to the comments in the markup below :

#wrapper {
    height: 100%;
    width:  100%;
    margin: 20px auto;
    display:table; /* added, so your div will behave like css-table */
    overflow: hidden;
}
#wrapper > .col {
    display: table-cell; /* abra-ka-dabra, equal height for all divs!!*/
    width: 30.3%;
    border:1px solid red;
    margin: 0 15px 3px;
    background-color: #fff;
    text-align: center;
    position: relative; /* required, because we need to place button @ he bottom of div*/
    height: 100%;
    -moz-box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
    -webkit-box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
    box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
}

    div.btn-align-bottom {
        position:absolute; /* place button at the bottom of div */
        bottom:50px; /* placing at proper position */
        left:0; /* needed for centering */
        right:0;/* needed for centering */
        border:1px solid yellow /* just to show you */
    }

UPDATE

If there are images in your markup as mentioned in a comment, ensure to include:

img{
    vertical-align:top; /* change default of baseline to top*/
}

view the functional demo

Answer №2

window.onload = function() {
    if(!window.location.hash) {
        window.location = window.location + '#';
        window.location.reload();
    }
}

This code will trigger a single reload of the page

Answer №3

The issue arises due to the elements being floated, causing the DOM to not recognize them as solid objects.

To resolve this problem, you will need to adjust the CSS accordingly.

#container {
    height: 100%;
    width: 100%;
    margin: 20px auto;
    overflow: auto;
}

#container > .box {
    display: inline-block;
    float: left;
    width: 30.3%;
    margin: 0 15px 3px;
    background-color: #fff;
    text-align: center;
    position: relative;
    height: 100%;
    -moz-box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
    -webkit-box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
    box-shadow: 0px 1px 2px rgba(48, 48, 48, 0.3);
    overflow: auto;
}
#container > .box > .content {
    padding: 0 0 35px;
    overflow: auto;
}

As for the jQuery aspect, you'll need to make the following adjustments:

var containerHeight = $("#container").innerHeight();
$("#container .box").each(function(){
    $(this).height(containerHeight+"px").css('overflow', 'hidden');
    $(this).find('.content').height((containerHeight-60)+"px").css('overflow', 'hidden');
});

Give it a shot and see if that resolves the issue.

Answer №4

The reason for this issue is that your script runs before the images have finished loading, making it impossible to accurately determine the innerHeight of #wrapper.

Your options are either specifying the image height in the DOM

OR

Executing this function on window onLoad.

Furthermore, you can eliminate the need for $.each:

var inHeight = $("#wrapper").innerHeight();
$("#wrapper .col").css("height", inHeight+"px");
$("#wrapper .col .content").css("height", (inHeight-60)+"px");

A more optimized approach (with wrapper caching):

var $wrapper = $("#wrapper");
var inHeight = $wrapper.innerHeight();
$wrapper.find(".col").css("height", inHeight+"px");
$wrapper.find(".col .content").css("height", (inHeight-60)+"px");

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

CSS class malfunction - various classes with identical functions

I'm new to the world of web development and design, embarking on a journey to learn all I can in these fields. Recently, I attempted to create unique hover styles for each menu button within my CSS classes-based menu list. However, I encountered an i ...

The Socket.io Chat application is indicating a memory leak with the EventEmitter, detecting 11 listeners that have been added. To resolve this issue

My private chat application is built using socket.io, node.js, and MySQL. However, I encountered an error when trying to use socket.on('example', function(data){...});. The error code thrown is related to a possible EventEmitter memory leak with ...

Extend and retract within a row of a table

I need help with modifying a dynamically generated table to meet specific requirements. The table currently looks like this. The task involves hiding all columns related to Category B, eliminating duplicate rows for Category A, and displaying entries from ...

Having trouble incorporating an API module into a nested component in Vue

I have encountered a perplexing issue when attempting to import an API module into a nested component within a Vue application. After simplifying the problem as much as possible, I created a codesandbox example to demonstrate it. The problem arises when ...

Unusual phenomenon in ASP.NET: Adding Debug=true in the web.config file results in CSS modifications

As the question suggests: After including debug=true in my web.config file (without making any other alterations to the code), my page is displayed without a background color. Can anyone shed light on why this might be happening? ...

css Apply styles to form controls only if they are not empty

Utilizing an angularjs form, I have customized the following example to fit my code: .my-5 { margin:100px; } .form-group { position: relative; margin-bottom: 1.5rem; } .form-control-placeholder { position: a ...

Utilizing Jquery for precise element placement and retrieving its position details

Within my bundle of jQuery code, there are a few areas where I am experiencing difficulties trying to recall functions. The following is an excerpt of the code: $(document).ready(function(){ $("#myTablePager").html(""); $.ajax({ type: "POS ...

Using Next.js to pass fetched data as props to components

I am currently working on integrating a leaflet map into my Next.js project. The map display is already functioning with predefined latitude and longitude in the component. However, I now need to show data retrieved from my API as the longitude and latitu ...

React JSX is failing to return either an Icon or String value depending on the input provided by the user for the password

I'm currently developing a registration page where I want to display a message saying "Your password must be at least 12 characters long" or show a green checkmark when the user types a password that meets this requirement. However, nothing is being d ...

What could be causing the absolute positioned element to not follow the positioning rules as expected?

Currently, I am following a guide on creating a website using Dreamweaver (). However, I have encountered a step in the tutorial that I am struggling to comprehend. The issue lies with an element named #navlink, which is absolutely positioned in the DOM s ...

Storing Code into mongoDB with the Help of CasperJS

What is the best way to save data scraped using casperjs into MongoDB? My casperjs script scrapes information from millions of websites and saves each site's content in its own folder. However, I have come to realize that it would be more efficient to ...

What is the best way to divide React Router into separate files?

My routes configuration is becoming cluttered, so I have decided to split them into separate files for better organization. The issue I am facing is that when using 2 separate files, the routes from the file included first are rendered, but the ones from ...

Determine whether the elements within an array are present on the webpage. If they are, display an alert. If not, reload the page

Initially, I had a userscript designed to search for a specific string within a webpage. If the script failed to locate the string, it would refresh the page after 4 seconds: var item = 'apple'; if(document.body.innerHTML.toString().indexOf(item ...

Updating the Hero Image

Scenario: In my project directory, under Product -> src -> static -> ostatic [ there are two folders named css and images ] -> images -> where all the images can be found. I am using bootstrap and attempting to set a background-image for j ...

Intermittent issue with Webdriver executeScript failing to detect dynamically created elements

It has taken me quite a while to come to terms with this, and I am still facing difficulties. My goal is to access dynamically generated elements on a web page using JavaScript injection through Selenium WebDriver. For instance: String hasclass = js.exec ...

Changing direction of arrow upon dropdown menu opening with JavaScript

I have developed a piece of code that enables users to click on a div in order to reveal a dropdown menu containing radio buttons. My goal is to make the arrows rotate 180 degrees when the dropdown menus are opened, and then return to their original positi ...

Eliminate the approximately 20 pixel space on the right side of the HTML email when viewed on iOS

Recently, I've been focused on developing a contact form for my portfolio website. Successfully sending HTML emails from the server to my email address has been a highlight. Since I mainly access my emails on my iPod Touch, I tailored the mail templat ...

Matching list symbols in regular expressions (Angular 2)

I have been attempting to find a solution for matching a list of symbols using regex, but I keep encountering errors in the result. The symbol list includes: !@#$+*{}?<>&’”[]=%^ if (text.match('^[\[\]\!\"\#&bs ...

Issue: The code is throwing an error "TypeError: Cannot read property 'push' of undefined" in the JavaScript engine "Hermes

Can anyone assist me with filtering an array of objects in a TypeScript React Native project using state to store array values and filter objects in the array? Having trouble with the following error in the mentioned method: LOG after item LOG inside ...

Text appears unclear and fuzzy when using Firefox browser

While my website looks fine on Chrome and Opera, it appears blurry on Internet Explorer and Firefox. I've tried researching and applying different CSS styles, but nothing seems to work. Both my Firefox and Internet Explorer versions are up to date, an ...