0
votes

I have a wordpress theme, which has a dynamic element, sized by javascrip(jquery)t on loading of the post (single post, not listing page)

so:

<div class='some-div'>

<div class='content'>
<?php /* code to load posts */ ?>
</div>

At the moment I get the height of div.content with javascript and apply that height to another div.some-div

The problem is on slow connections the height calculations are done before the content has loaded

So, what I need is to run a javascript function ONLY once the post has finished loading

At the moment Im delaying the function, have the script at the footer, and use the 'defer' attribute, but I still happens

any ideas?

3
When you say you're "delaying the function" does that mean you are running the code on $(document).ready? - CrazyWebDeveloper
Doh, Nope like a fool I was running the code outside of the doc ready. - atmd

3 Answers

0
votes

Why don't you try to save first the content of that div into a variable like

var my_js_var = <?php echo $content ?> ;

Then just say with a function to call the resize div function ONLY when the content stored into your div is equal to your 'my_js_var'

//Would be something like:
var my_content = <?php echo $content ?> ; //Downloading the content

//Timer to constantly check if the div is loaded correctly
var timer = window.setTimeout('refresh()',100)

function refresh () {
//if the content of your div is equal to 'my_content' variable
//then call the resize function of the div
//finally clear the timeout [clearTimeout(timer)]

}

Didn't test it out

Tell me if it works..

Best regards

0
votes

You may run your calculations in the onload event to make sure that the images, texts etc. in your post has finished loading.

<body onload="YourCalculationFunctionHere">

Don't use the .ready() method of jquery for your calculations bacause it will run after the DOM was loaded not after the page(inc images, texts etc.) was loaded.

http://www.w3schools.com/jsref/event_onload.asp

0
votes

Just to answer incase anyone has the same issue.

I was running the function before the dom had loaded, a simple mistake easily solved.

use

$(function(){
  // code here
});

as a short hand for $(document).ready