0

I'm new to web development, and I have a problem I can't solve.

Problem: I need to show content after loading that content.

## file.js ##
//on click this code run!
//and after click i see web site load content

var toInsert = $('<div class="togllecomposition tg'+number+'">\
                                <div class="infoclip">'+info+'</div>\
                                <iframe width="420" height="315" src="'+clip+'" frameborder="0" allowfullscreen></iframe>\
                                <div class="comments">\
                                    <div class="commentlabel">Комментарии:</div>\
                                    <div class="comment">\
                                        <div id="hypercomments_widget'+id_composition+'"></div>\
                                            ' + _hcwp.push({widget:"Stream", widget_id: ID, xid: id_composition, append:"#hypercomments_widget"+id_composition, hc_disabled: 1}) +'\
                                    </div>\
                                </div>\
                            </div>');

//this didn't work
$(toInsert).load(function() {
     $(".composition".concat(number)).after(toInsert); 
 });


// this work but with lags because content in process of loading
 $(".composition".concat(number)).after(toInsert);

How can I solve this problem?

kzvonov
  • 5
  • 4

1 Answers1

0

load() event is dangerous and ambiguous, because you are listening load event and load an ajax content simultaneously. Please, try this:

  var toInsert = $('video from youtube');

  $(toInsert).on("load", function() {
      $(".composition"+concat(number)).after(toInsert); 
  });

You can use a deferred in a different way:

  $(toInsert).load('url-to-video', function(response, status, xhr ) {
       console.log(response, status, xhr );
  });

These are two different ways to do it. Don't mix both!

EDIT

Viewing the edit of OP, I see an iframe that may be listened, and not the DOM element.

  toInsert.find('iframe').on("load", function() {
      $(".composition".concat(number)).after(toInsert); 
  });

Here you are the explanation because you don't must to use load() method to listening load event:

http://api.jquery.com/load/

It makes an AJAX query.

squarefrog
  • 4,750
  • 4
  • 35
  • 64
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69