Webflow sync, pageviews & more.
NEW

Webflow & Javascript: How can I hide search result items with a blank page in Webflow that contain "/sku/" in the URL using Javascript?

TL;DR
  • Use JavaScript to target .w-dyn-item elements with links containing /sku/, then fetch the linked pages.
  • If the fetched page is empty or throws an error, hide the corresponding search result item.

To hide search result items in Webflow that both (a) have a /sku/ in their URL and (b) link to a blank page, you can use JavaScript to filter these elements after the search results load.

1. Understand the Structure

  • Webflow search results are rendered in the DOM after the page loads, using a standard structure like .w-dyn-item for each result.
  • Each result usually contains a link (e.g., <a href="...">...</a>) to the results page.
  • You're targeting links that include /sku/ in the URL and return a blank or empty page, likely indicating a 404 or empty content result.

2. Add Custom JavaScript

You can add the following script to your Page Settings > Before tag or use an Embed element on the search results page:

  • The script will:
  • Select all search result link elements.
  • Check if their href contains /sku/.
  • Fetch a portion of the linked page to detect if it's blank (no meaningful content).
  • If it meets these conditions, it hides the entire search result item.

3. Use JavaScript to Filter Items

Paste this in your custom code area:

document.addEventListener("DOMContentLoaded", function () {  const resultItems = document.querySelectorAll(".w-dyn-item");  resultItems.forEach(item => {    const link = item.querySelector("a[href]");    if (link && link.href.includes("/sku/")) {      fetch(link.href)        .then(response => response.text())        .then(html => {          const parser = new DOMParser();          const doc = parser.parseFromString(html, "text/html");          // Customize this selector to target actual page content          const mainContent = doc.querySelector("main") || doc.body;          if (!mainContent || !mainContent.textContent.trim()) {            item.style.display = "none";          }        })        .catch(() => {          // On error (e.g. 404), hide the item          item.style.display = "none";        });    }  });});

4. Customize Content Detection (Optional)

  • Replace doc.querySelector("main") with a more specific selector if your main page content is elsewhere (e.g., .page-content, .section, etc.).
  • This avoids false positives on valid /sku/ pages that have real content.

Summary

To hide search results linking to blank /sku/ pages, use JavaScript to:

  • Check if URL includes /sku/
  • Fetch the page content
  • Hide the result item if the page is empty or returns an error

This ensures cleaner, more relevant search results without broken or placeholder pages.

Rate this answer

Other Webflow Questions