๐Ÿ” JavaScript Utilities & Miscellaneous
Estimated reading: 4 minutes 10 views

โš™๏ธ JavaScript โ€“ Lazy Loading, Printing, and Dialogs: Complete Guide with Examples


๐Ÿงฒ Introduction โ€“ Why These Features Matter in Modern Web Apps

Whether you’re optimizing load performance, enabling print views, or enhancing UX with native dialogs, these three JavaScript featuresโ€”Lazy Loading, Printing, and Dialogsโ€”are powerful tools every front-end developer should master.

By the end of this article, you’ll learn:

  • ๐Ÿ’ค How to defer resource loading with Lazy Loading
  • ๐Ÿ–จ๏ธ How to generate and trigger Print Views
  • ๐Ÿ’ฌ How to use built-in Dialog boxes (alert, confirm, prompt) and the modern <dialog> element

๐Ÿ’ค Lazy Loading in JavaScript

โœ… 1. What is Lazy Loading?

Lazy loading delays loading non-critical resources until they are needed (like images below the fold or heavy scripts). This boosts page speed and improves user experience.


โœ… 2. Lazy Loading Images with HTML

<img src="thumbnail.jpg" loading="lazy" alt="Lazy Image Example" />

โœ… Explanation:

  • loading="lazy" tells the browser to load the image only when it’s about to appear in the viewport.

๐Ÿ’ก Tip: Native lazy loading works in most modern browsers. Always provide a fallback for unsupported browsers.


โœ… 3. Lazy Load with JavaScript and IntersectionObserver

const images = document.querySelectorAll("img[data-src]");

const observer = new IntersectionObserver((entries, obs) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      const img = entry.target;
      img.src = img.dataset.src;
      obs.unobserve(img);
    }
  });
});

images.forEach(img => observer.observe(img));

โœ… Explanation:

  • IntersectionObserver watches when elements enter the viewport.
  • When visible, the real image URL (data-src) is assigned to src.

โš ๏ธ Note: Always check if the browser supports IntersectionObserver.


๐Ÿ–จ๏ธ Printing Web Pages with JavaScript

โœ… 1. Trigger Print Dialog

window.print();

โœ… Use case: One-click print button for invoices, receipts, or reports.


โœ… 2. Add a Print Button

<button onclick="window.print()">๐Ÿ–จ๏ธ Print this page</button>

๐Ÿ“˜ Add this inside HTML where needed.


โœ… 3. Print-Specific CSS (Best Practice)

@media print {
  body {
    font-size: 12pt;
  }
  .no-print {
    display: none;
  }
}

๐Ÿ’ก Tip: Use .no-print classes to hide elements like nav bars, ads, or buttons during printing.


๐Ÿ’ฌ JavaScript Dialogs โ€“ Built-in & Custom


โœ… 1. alert(), confirm(), and prompt()

๐Ÿ”น alert()

alert("Operation completed!");

โœ… Shows a simple info dialog with OK button.


๐Ÿ”น confirm()

if (confirm("Are you sure you want to delete this?")) {
  console.log("Deleted!");
}

โœ… Returns true or false based on user action.


๐Ÿ”น prompt()

const name = prompt("Enter your name:");
console.log("Hello, " + name);

โœ… Accepts user input through a modal dialog.

โš ๏ธ Warning: Native dialogs are blocking and halt script execution until dismissed.


โœ… 2. Modern Dialogs using <dialog> Element

๐Ÿ”น HTML

<dialog id="myDialog">
  <p>This is a custom dialog box.</p>
  <button id="closeBtn">Close</button>
</dialog>
<button id="openBtn">Open Dialog</button>

๐Ÿ”น JavaScript

const dialog = document.getElementById("myDialog");
document.getElementById("openBtn").onclick = () => dialog.showModal();
document.getElementById("closeBtn").onclick = () => dialog.close();

โœ… Modern, non-blocking alternative to alert().

๐Ÿ’ก Tip: <dialog> is customizable with CSS, unlike alert or confirm.


๐Ÿ“Š Comparison Table

FeatureUse CaseBest MethodSupport
Lazy LoadingImage & resource optimizationloading="lazy" / IntersectionObserverModern browsers
Print ViewDocuments, reports, receiptswindow.print() + @media printAll browsers
Dialog BoxesAlerts, confirmations, inputs<dialog> or alert()/prompt()<dialog> = Modern

โ™ฟ Accessibility & Performance Tips

  • ๐Ÿ’ค Lazy loading improves first paint and LCP (Largest Contentful Paint) for SEO.
  • ๐Ÿ–จ๏ธ Always design a print-specific layout to ensure proper formatting.
  • ๐Ÿ’ฌ For <dialog>, use aria-label, tabindex, and focus trap to ensure accessibility.

โœ… Summary โ€“ What Youโ€™ve Learned

In this article, you learned how to:

  • ๐Ÿ’ค Use Lazy Loading to improve page speed by deferring image/resource loads
  • ๐Ÿ–จ๏ธ Add Print functionality with styled output using JavaScript and CSS
  • ๐Ÿ’ฌ Implement JavaScript Dialogs from simple alerts to custom modal dialogs

These tools enhance performance, usability, and interactivity of any modern web app or dashboard.


โ“ FAQ โ€“ Lazy Loading, Print, Dialogs in JavaScript

โ“ What is the easiest way to lazy load images in HTML?
๐Ÿ‘‰ Use loading="lazy" in the <img> tag for native lazy loading.

โ“ Can I control what prints using CSS?
๐Ÿ‘‰ Yes. Use @media print rules and add .no-print classes to hide elements like navbars or ads.

โ“ Are browser dialogs like alert() accessible?
๐Ÿ‘‰ No. They’re not screen-reader friendly and block script execution. Prefer <dialog> with proper ARIA roles.

โ“ Is <dialog> supported in all browsers?
๐Ÿ‘‰ Most modern browsers support it. For older ones, polyfills are available.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

JavaScript โ€” Lazy Loading / Printing / Dialogs

Or Copy Link

CONTENTS
Scroll to Top