Javascript Not Running Until Refresh

By : Liz

I have used this javascript snippet to create a bunch of divs with equal height before and have never had this problem before, but for some reason the code isn't running until I refresh the page. Here is a sample instance:

<div class="container">
  <div class="row text-center">

    <%= link_to "" do %>
      <div class="col-sm-6 col-md-4">
        <div class="site-preview-box">
          <h2>WIP: Manly Art of BBQ</h2>
          <h4>Branding, Logo Design, UI/UX Design, Web Development</h4>
          <%= image_tag 'portfolio_mab.png', class: "portfolio-image" %>
          <p><a href="">The Manly Art of BBQ</a> is a humorous website that gives information on a variety of "manly" topics.</p>
          <p><strong>This site has a vast array of user-submitted content sorted in a Reddit-like fashion, as well as a few online "tools" that were a lot of fun to develop.</strong></p>
        </div> <!-- site preview box -->
      </div> <!-- column box -->
    <% end %>

    <%= link_to "" do %>
      <div class="col-sm-6 col-md-4">
        <div class="site-preview-box">
          <h2>WIP: OC Outreach</h2>
          <h4>Branding, Logo Design, UI/UX Design, Web Development</h4>
          <%= image_tag 'portfolio_oco.png', class: "portfolio-image" %>
          <p><a href="">OC Outreach</a> is a non-profit community organization that operates in the Orange County area.</p>
          <p><strong>This was a fairly simple site functionality-wise, but it was cool to design every aspect of a site (logos, design, etc.) from the ground up.</strong></p>
        </div> <!-- site preview box -->
      </div> <!-- column box -->
    <% end %>

  </div> <!-- row -->

</div> <!-- page container -->

  $( document ).ready(function() {
      var heights = $(".site-preview-box").map(function() {
          return $(this).height();
      maxHeight = Math.max.apply(null, heights);
      window.onresize = function(){ location.reload(); }

Instead of equally-tall divs, I end up with this:

Problem Example Image

Then, when I hit refresh (sometimes it takes multiple refreshes), it corrects to this:

Problem Fixed Image

Any ideas on how to get this to work before I refresh the page?

ADDITIONAL INFO This is not solved by the answer for this question. I tried substituting load for ready and it renders the javascript nonfunctional for this purpose.

By : Liz


You can give a css width and height to images. So boxes will look always as you wish. In your case, browser caches images in first time then their appear fix itself in second time because of reading images from browser's cache.

Make your divs appear after all images are finished loading. The divs are the correct size on reload because at that point all the images are cached by your browser and the JavaScript can pick up the appropriate size.

In jquery change your ready to this:

$(window).on("load", function() { // weave your magic here. });

should fix it. If not.

Something like this should help as a rough example.

var img = new Image();
img.onload = function() { alert("Height: " + this.height); }
img.src = "http://path/to/image.jpg"

based on the structure of the example, it's likely that webCrtPartList is NOT the managed entity. You need to use the response of EntityManager.merge() to get the managed entity (the parameter to EntityManager.merge() will not [necessarily] be managed)

This video can help you solving your question :)
By: admin