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:
- Define
yourScriptContentwith your actual script content. - 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. - If the script content is not found on the page, create a new
<script>element, set itsinnerHTMLto 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.