views:

274

answers:

2

I'm loading remote data using dynamic script tags and JSON. the remote page that I'm displaying on my website has a div in it that I use to load content into.

The problem is the javascript functions do not see the div as the page loads because it is remote data. if I set a timeout of about 300 it usually works and my javascipt can see the div. But sometimes it takes longer and it breaks the javascript.

I'm tring this:

function load_content()
{
  if(document.getElementById('remote_div')==null)
  {
  setTimeout('load_content()', 300);
  }
  else
  {
  document.getElementById('remote_div').innerHTML = 'Content goes here'
  }
}

but it just doesn't seem to work... what is wrong with it?

+1  A: 

You may want to do this using setInterval. Something like:

var intrval = setInterval( function(){
  if(document.getElementById('remote_div')) {
      load_content();
       clearInterval(intrval);
 }, 50);

function load_content() {
   //loading content here
}

This way you don't have to estimate the loading time. load_content is executed when div#remote_div can be found in the DOM tree.

Edited based on comments, forgot to assign the interval, so it wouldn't ever clear indeed.

KooiInc
That's basically the same thing as using `setTimeout`. You have to keep the return value from `setInterval` so that you can use it in `clearInterval`, otherwise you will fail to stop it and it will keep updating the page 20 times a second.
Guffa
@Guffa: Notice how he called `clearInterval`: intervals are numbered, and if this is the (number)th one, then you can just pass that number. Of course, that assumes you are keeping and can keep track of all intervals used (eg, no script libraries brought in).
Anonymous
@Guffa: I don't think it's the same. If after the timeout value of setTimeout the div is not present, that's the end of it. Using an intervalled call, at some time the div will be present and can be used. I edited the answer, so now the interval is decently cleared.
KooiInc
@Anonymous: Even if they in practice are numbered, I don't find any documentation that specifies this, only that it returns an Id that can be used with clearInterval. So, you should not depend on that implementation detail.
Guffa
@Kooilnc: Yes, it's the same. The method calls itself until the element is found, so it doesn't end after the first iteration.
Guffa
@guffa: I think we understand each other wrong: you probably mean the interval function basically does the same as the load_content function. That's right, it does. I understood however: setInterval is the same as setTimeout. That should not be right. If somebody convinces me it is, I will never program any javascript again.
KooiInc
@Kooilns: Yes, that is correct. Sorry if I was unclear.
Guffa
Timer allocation in this manner was how Netscape did it, so competing browsers copied the behavior to remain compatible with existing script. I doubt you'll find a js implementation that comes anywhere near a browser that doesn't do it that way (including AS), though being practically undocumented alone in any case makes it unrecommended procedure.
Anonymous
By using Chrome's resources developer tool, I think I have found the error only occurs if jquery UI loads before jquery... So how do I make sure jquery loads, then I can load jquery UI?currently I load them at the same time, but half the time jquery loads fine from Google CDN, but the other have it takes longer then the UI
Matt
@http://stackoverflow.com/users/97408/anonymous: I just do not understand what you want to say. Especially your last sentence is abacadabra for me. May be because I'm dutch, or maybe my intelligence is mediocre (both being true by the way), but I really can't make anything of it. It sounds bloody interesting though ...
KooiInc
Well, I got it to work on Chrome and FF... using .onreadystatechange and .onload when loading jquery... IE it don't work (typical)
Matt
A: 

Are you using iframe?

If so, try

document.getElementById('YOUR_IFRAME_ID').contentWindow.document.getElementById('remote_div')
resopollution
I'm not using iframes... I'm strictly XSS using dynamic script tags with callbacks. I don't have any control over the websites my content is displayed on, so I need a way to get around the browser security of remote data and ajax. I also need dynamic height resizing.Everything works great, but now trying load a jquery slider and it only appears half the time. When it doesn't appear I get this error: Uncaught TypeError: Object #<an Object> has no method 'slider'I have to load jquery, ui, and slider css... I need a way to make sure these are loaded before processing the DOM
Matt