0

I am testing this simple code below and it works...

$(document).ready( function() {
   $('.show-modal').click( function() {
        $('#modal-box').css( {
            left: $(window).width() / 2 - $('#modal-box').width() / 2,
            top: $(window).height() / 2 - $('#modal-box').height() / 2
        } );
        //delay(openModal, 500);
        openModal();
   } );

} );

However when I run the code below it does not, all I have done is removed the click event and I was hoping this would load on page load instead of needing a click event but it is not working. Any ideas?

$(document).ready( function() {

        $('#modal-box').css( {
            left: $(window).width() / 2 - $('#modal-box').width() / 2,
            top: $(window).height() / 2 - $('#modal-box').height() / 2
        } );
        //delay(openModal, 500);
        openModal();

} );

As requested, the openModal function

openModal = function () {
    $('#modal-box').show();
    $('#modal-mask').show();
};
2
  • can you show what openModal() does? Commented Apr 2, 2012 at 8:23
  • @NiftyDude I have added it above Commented Apr 2, 2012 at 8:25

3 Answers 3

3

Mabye you need all resources to be loaded instead of just the dom. For this use:

$(window).load(function () {
  // images and stuff will be loaded
});
Sign up to request clarification or add additional context in comments.

2 Comments

if this fixed it, can you accept the answer so @yoshi can get the rep points. cheers!
@booyaa I always do, it makes you wait 10 minutes though
1

Try this:

$(document).ready( function() {

        $('#modal-box').css("left", (($(window).width()/2) - ($('#modal-box').width()/2)) + 'px');
        $('#modal-box').css("top", (($(window).height()/2) - ($('#modal-box').height()/2)) + 'px');

        //delay(openModal, 500);
        openModal();

} );

Comments

-1

Browsers usually block popped up modals when they are opened onLoad of a page. That explains why it works when you do it on click. Try disabling your pop up blockers.

1 Comment

It is a div and not a new Window. Only new popup windows will be blocked.

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.