Adding the script to the body only If it is not present on the website.

Add a script to your Suite Commerce Advanced (SCA) website’s layout component, but you want to check if the script is already present on the website before appending it to the <body>. You can do this by checking if the script’s content or some unique identifier is present on the page. Here’s a high-level approach:

// Your script content
var yourScriptContent = '...'; // Replace with your actual script content

// Check if the script content is already on the page
if (document.body.innerHTML.indexOf(yourScriptContent) === -1) {
  // If the script content is not found on the page, add it to the body
  var scriptElement = document.createElement('script');
  scriptElement.innerHTML = yourScriptContent;
  document.body.appendChild(scriptElement);
}

In this code:

  1. Define yourScriptContent with your actual script content.
  2. Use document.body.innerHTML.indexOf(yourScriptContent) to check if the script content is already present in the <body> of the webpage. If it returns -1, it means the script content is not found on the page.
  3. If the script content is not found on the page, create a new <script> element, set its innerHTML to your script content, and append it to the <body>.

This way, the script will only be added to the page if it’s not already present, helping you avoid duplicate scripts.

Leave a comment

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