On PageLoad show Magento Static block in ajax

On PageLoad show Magento Static block in ajax

September 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
On PageLoad show Magento Static block in ajax

Displaying Magento Static Block as a Pop-up on Page Load

Hello developers! Today we’re going to tackle an interesting challenge: displaying a static block in Magento as a pop-up window upon page load. This is particularly useful for one-time promotions or subscription forms.

What You’ll Learn

By the end of this tutorial, you will:

  1. Add Lightbox CSS and JavaScript files.
  2. Create a link to your desired static block.
  3. Configure cookies for expiration.
  4. Solve common issues like calling a static block in a pop-up.

Prerequisites

Before we dive in, ensure the following:

  • You are running Magento 1.7 or later.
  • Your site is a fresh copy with no prior modifications to core files.
  • You have access to upload and modify template files via SFTP/SSH.

Step-by-Step Guide

Add Lightbox CSS & JavaScript Files

First, let’s incorporate the Lightbox library into your theme. For this example, we’ll use the default Magento theme (rwd).

  1. Navigate to app/design/frontend/rwd/default/layout/local.xml.

  2. Inside the <layout> tag, add the following:

<default>
  <reference name="head">
    <action method="addJs"><file>lightbox/lightbox.js</file></action>
    <action method="addCss"><file>lightbox/lightbox.css</file></action>
  </reference>
</default>
  1. Save and close the file.

Next, we need to create a link in your template that will trigger the pop-up with our static block content.

  1. Open app/design/frontend/rwd/default/template/page/html/header.phtml.

  2. Inside the <body> tag, add the following line:

<a href="<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('newsletter_subscribe')->toHtml(); ?>" rel="lightbox">Subscribe to Our Newsletter</a>

Replace 'newsletter_subscribe' with your actual block identifier.

Configure Cookies for Expiration

To ensure the pop-up appears only once and within a specific time frame, we’ll configure cookies. Magento handles this natively via the core/session configuration node.

  1. Open app/etc/local.xml.

  2. Locate <config><global>.

  3. Inside <session_save> (or add it if not present), set the cookie lifetime:

<session_save>
  <cookie_lifetime>604800</cookie_lifetime> <!-- 1 week in seconds -->
</session_save>

Resolving Common Issues

Calling a Static Block in a Pop-up

The issue you faced likely stems from not setting the rel attribute correctly. Ensure your link looks like this:

<a href="<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('newsletter_subscribe')->toHtml(); ?>" rel="lightbox">Subscribe to Our Newsletter</a>

Double-check the following:

  • Ensure you have uploaded both lightbox.js and lightbox.css into your theme’s JavaScript folder.
  • Verify your template (.phtml) file is in the correct location.
  • Confirm there are no syntax errors or missing semicolons.

Conclusion

That’s it! You now know how to display a Magento static block as a pop-up window on page load, with cookies expiring after a specified period. This technique can be applied across various themes and blocks for different purposes.

Remember to always test changes in a development environment before deploying them live.

If you found this tutorial helpful, please let us know! We’d love to hear your feedback. Additionally, check out our other Magento tutorials at Laravel Company.

Happy coding!

Magento 1 API Themes Configuration

Our network