Search code examples
magentocolorbox

Using the colorbox for pop-up immediately open (jQuery - Magento)


I'm using Jquery colorbox to implement a popup windows. This pop-up is immediately open and it's working. But for the first loading page, just the first loading, the pop-up can't load the content. My screen

jQuery(document).ready(function defaultPopup(){

    var direct = '<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('popup')->toHtml(); ?>'
    if(direct){
        jQuery('#popup_home').colorbox({open:true,html:direct,overlayClose:false});
        return false;
    }
});
<div id="popup_home"></div>

Solution

  • You should escape special characters (<>) in your string.

    For the web browser the content of your direct variable is an HTML tag without content.

    Try this:

    jQuery(document).ready(function defaultPopup(){
    var direct = '<?php echo $this->getLayout()->createBlock(\'cms/block\')->setBlockId(\'popup\')->toHtml(); ?>'
    direct = $('<div/>').text(direct).text() // escaping characters in the initial string
    if(direct){
        jQuery('#popup_home').colorbox({open:true,html:direct,overlayClose:false});
        return false;
    }
    });
    <div id="popup_home"></div>