Thomas
Thomas

Reputation: 5089

Get Jquery modal box to appear on page load

I need to get this Jquery box to appear on page load. Right now it only appears when the user clicks a link.

Here is the trigger:

    <script type="text/javascript"> 

$(document).ready(function() {  

    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();

        //Get the A tag
        var id = $(this).attr('href');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect     
        $('#mask').fadeIn(1000);    
        $('#mask').fadeTo("slow",0.8);  

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(2000); 

    });

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();

        $('#mask').hide();
        $('.window').hide();
    });     

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });         

});

</script> 

Im sure there must be some sort of solution here, any ideas?

Upvotes: 0

Views: 2866

Answers (3)

hoelzcarol
hoelzcarol

Reputation: 11

code running with the function close:

function openModal(id) {

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});

    //transition effect
    $('#mask').fadeIn(1000);
    $('#mask').fadeTo("slow", 0.8);

    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();

    //Set the popup window to center
    $(id).css('top', winH / 2 - $(id).height() / 2);
    $(id).css('left', winW / 2 - $(id).width() / 2);

    //transition effect
    $(id).fadeIn(2000);

    $('.window .modalClose').click(function (e) {
        e.preventDefault();

        $('#mask').hide();
        $('.window').hide();
    });     

    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });         

}

$(document).ready(function() {

    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        openModal($(this).attr('href'));
    });

    openModal('#modalDialog');

});

Upvotes: 1

fantactuka
fantactuka

Reputation: 3334

You can fire click event on the necessary link

$(window).load(function() {
    $('a#initialModalLink[name=modal]').trigger("click");
});

Where initialModalLink is id of the link that should execute modal window. Also (and it would be better) you can extract modal window opening into separate function and use it:

function openModal(id) {

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});

    //transition effect
    $('#mask').fadeIn(1000);
    $('#mask').fadeTo("slow", 0.8);

    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();

    //Set the popup window to center
    $(id).css('top', winH / 2 - $(id).height() / 2);
    $(id).css('left', winW / 2 - $(id).width() / 2);

    //transition effect
    $(id).fadeIn(2000);

}

$(document).ready(function() {

    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        openModal($(this).attr('href'));
    });

    openModal('the href of initial modal box');

});

Upvotes: 1

David Hellsing
David Hellsing

Reputation: 108500

Sure, you just need to know what href to send. You can extract the modal function like this and call it when needed:

var modal = function(id) {

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});

    //transition effect     
    $('#mask').fadeIn(1000);    
    $('#mask').fadeTo("slow",0.8);  

    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();

    //Set the popup window to center
    $(id).css('top',  winH/2-$(id).height()/2);
    $(id).css('left', winW/2-$(id).width()/2);

    //transition effect
    $(id).fadeIn(2000); 

});

$(document).ready(function() {
    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        e.preventDefault();
        modal($(this).attr('href')); // this might be a typo, should be id?
    });
    // fire the modal on load
    modal('http://display.me/in/modal/');
});

Upvotes: 0

Related Questions