In this post we’re going to cover how we code create AJAX like post loading in popup without writing code for AJAX.
AJAX (Asynchronous JavaScript and XML) is a web development technique used to create interactive and dynamic web applications. It allows web pages to update content asynchronously by exchanging small amounts of data with the server in the background, without requiring a full page reload.
Here’s a breakdown of how AJAX works:
XMLHttpRequest
or newer fetch
API) sends a request to the server.<iframe>
?An <iframe>
(inline frame) is an HTML element that allows embedding an entire HTML document (or web page) within another web page. The content inside the iframe is treated as a separate document, and can be loaded independently.
<iframe>
and AJAX:Feature | <iframe> | AJAX |
---|---|---|
Content Type | Loads an entire HTML page. | Can fetch specific data (like JSON, XML, or HTML fragments). |
Page Reload | Loads and displays a new page within the frame. | Updates parts of the page without a full page reload. |
Control | Limited control over the content and how it’s displayed (especially from different domains). | Full control over how data is used or displayed. |
Interactivity | Interaction between parent page and iframe is more limited (cross-origin restrictions apply). | Highly interactive, allowing seamless integration between the client-side script and server-side data. |
Performance | Can be slower because it loads an entire HTML page. | More efficient, fetching only the data you need. |
Security | Isolates content in a separate context, which can be more secure in some cases. | Data is fetched and processed by JavaScript, which may introduce security risks if not handled properly (e.g., XSS). |
Mostly users of the page builder are no-code or low-code persons and they are not full stack developers. To handle AJAX properly you need full stack skills to make backend and frontend functionality. That is often too much for design oriented page builder users.
Also, above parts are much harder and time consuming to do than to make conditions and templates with page builder. Conditions and Templates could be done in matter of minutes, and AJAX could eat up whole day to implement, style and test.
With combination of Conditions and Templates within Breakdance we could solve make iframe to execute almost all benefits of the AJAX approach, reduce time to implement, and neglects cons of iframe method.
We are using URL parameter to differentiate when post should be loaded as regular or as iframe.
When loading post AJAX-like we don’t want to have header and footer because they are already present on main page and that looks ugly. Luckly to get rid of them is really easy, we need to create two special templates.
Make header template, with blank content. So to hide header we are just loading empty template instead of regular one.
Use following settings for header templates:
Settings | Value |
Applies to | Everywhere |
Priority | 2000 |
Condition: Dynamic data > URL Parameter | (is) loadoveriframe=true |
Repeat the same for footer template.
Logical idea behind this is that every time this URL parameter is present, we’ll place blank header and footer and remove them in this fashion from iframe.
Condition is here in place because without it we would have whole site without header and footer templates which we surly don’t want.
In order to to have
Settings | Value |
Applies to | *Note1 |
Priority | 2000 |
Condition: Dynamic data > URL Parameter | (is) to loadoveriframe=true |
*Note1 – if you apply everywhere it will use that template, but unlike header and footer you may want it to apply only to post, or one CPT or something like that. This would give you flexibility to make variation for each.
If your cache plugin supports adding cache keys, you could define loadoveriframe URL parameter as key so cache would cache that version also and they would not collide with regular post template. And force cache when this parameter is present.
Most of the cache plugins should skip cache when URL parameter is present. And by default regular post template will be cached and special won’t. But all of this will depend on your cache system.
We’ve reinvented this approach to enable our users to ship AJAX-like interface in matter of minutes and that they would have fully flexibility over design without need to write almost any code.
We are using this approach for Dynamic Lightbox component. This component aims to load post