0

In my code I have external script that adds some element to my page. This script loads async after document.ready:

<script src="http://any.com/script.js"></script>

This script contains next:

$.ajax({
       url: '/anyScript',
       complete: function(){
          alert('yo'); // FIRED
          $('body').append('<div id="xxx" />'); // FIRED
       }
   });

I need to wait until this element will appear and add some styles to it

$(function(){

    $('body').on('load','#xxx', function(){
        $(this).css({
            background:'red',
            width: 100,
            height: $('#first_el').height()
        });
    });
});

This doesn't fire. What to do?

UPDATED: http://jsfiddle.net/81ucdoLo/1/

0

4 Answers 4

1

This solution is based on the assumption that you don't have any control over the external script. So the proposed solution is to use an interval based solution to check whether the target element is loaded if so style it and then stop the interval.

In that case, try use $.getScript() to load the script like

jQuery.getScript('http://any.com/script.js', function () {
    var interval = setInterval(function () {
        var $el = $('#xxx');
        if ($el.length) {
            clearInterval(interval);
            $el.css({
                background: 'red',
                width: 100,
                height: $('#first_el').height()
            });
        }
    }, 500);
})

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I think the only one way to make interval. This is work for me. Thank you!
1

You can try using ajaxComplete as shown :

$( document ).ajaxComplete(function() {
  $('#xxx').css({
    background:'red',
    width: 100,
    height: $('#first_el').height()
 });
});

Working Demo

2 Comments

Good solution. But it doesn't help me. It can be not ajax request, and xmlHttprequest or timeout or I can have a lot of different ajax requests. So I only need to listen when element will appear
@SergeyKudryashov..okk but as per your question and fiddle where you are showing just ajax request .. i think this is the most compact and useful answer..i think you should update your question and specify completely what else do you want.
0

This should wait for the element to be ready:

$(function(){
  $("#xxx").css('top','123px');
  //OR
  $("#xxx").addClass('topMargin');
});

1 Comment

Updated. The value is calculated by js. And script loads after document is ready.
0

Do somthing like this, call your js function using window.onload, it will execute doSomthing function after your page load.

window.onload = function() {doSomthing();}

function doSomthing()
{
   $("#xxx").css('top','123px');

}

Or add timeout,

setTimeout(doSomthing,1000);

this will delay the call process, and will call after specified time.

What if you try this : JSFiddle Demo:

I updated your demo.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.