-
ReplaceHTML doesn't work in Web Experiences with Sitecore Personalize
February 6, 2022 Rodrigo Peplau 0
While creating a Web Experience in Sitecore Personalize, I wanted to use the replaceHTML function in the Javascript part of my Variant, but it was not working.
replaceHTML('#main-component');
My original HTML has a div like this:
<div id="main-component">Temporary content</div>
And I was trying to completely replace the whole div with the markup from my Variant
<div id="main-component"> <h1 class="title">Hello World! - Variant 1</h1> </div>
While the methods insertHTMLBefore and insertHTMLAfter were working normally, nothing was happening with replaceHTML.
But why?
Debugging the Javascript method, I’ve noticed the error is due to what looks to be a bug in the native replaceHTML function.
function replaceHTML(e) { var n = document.querySelector(e) , t = "bx-" + variant.ref; if (document.getElementById(t)) document.getElementById(t).innerHTML = variant.assets.compiled.html; else { var r = document.createElement("div"); if (r.setAttribute("id", "bx-" + variant.ref), r.innerHTML = variant.assets.compiled.html, !(n instanceof HTMLElement)) throw "HTML element not found"; n.innerHTML(r) // <=== The "bug" is here } }
More specifically in the bottom of the block, the n.innerHTML is being called as a function, when it’s actually a string property.
I’m not sure what this line is really trying to do here, but I’ve managed to fix the “bug” with a corrected version of the replaceHTML function.
The “fix”
To fix the issue I’ve copied the original function to my target page (preferably at the bottom, before the closing body tag).
<script> function replaceHTML2(e, variant) { var n = document.querySelector(e) , t = "bx-" + variant.ref; if (document.getElementById(t)) document.getElementById(t).innerHTML = variant.assets.compiled.html; else { var r = document.createElement("div"); if (r.setAttribute("id", "bx-" + variant.ref), r.innerHTML = variant.assets.compiled.html, !(n instanceof HTMLElement)) throw "HTML element not found"; n.outerHTML = r.outerHTML; // <=== the "fix" is here } } </script>
Now of course you’ll need to call the new function in your Variation Javascript, instead of the original one. Notice that the variant has to be also passed to the new function as a parameter, as the variant context is not accessible from where the function is declared.
replaceHTML2('#main-component',variant);
After these changes, I’m now able to actually replace any markup from the original page with markup from the Web Experience.
Do you have any thoughts on this strange situation? Please leave me a word!
Categories: CDP/Personalize
Sitecore Personalize: Difference between "Experiences" and "Experiments" Glossary: Sitecore CDP vs Sitecore XP
Proudly 9x Sitecore MVP
(2016-2024)
Localization
Recent Posts
Recent Comments
- navan on Meet MVPinny: the AI-Powered Sitecore Assistant
- Adriana on Content generation with Sitecore Connect and ChatGPT
- NAVAN on Automatic Sitecore NuGet upgrades with Powershell
- Hedipo S Menezes on Corey Peplau wrote this - WFFM conflict with Unity DI and a lesson on how Sitecore community is so amazing
- Rodrigo Peplau on ERROR [Content Testing]: Cannot find PhantomJS executable at ' (...) /data/tools/phantomjs/phantomjs.exe'. Aborting screenshot generation.
Archives
- June 2024
- April 2024
- February 2024
- December 2023
- November 2023
- August 2023
- July 2023
- January 2023
- February 2022
- December 2021
- November 2021
- March 2021
- July 2020
- February 2020
- September 2019
- July 2019
- April 2019
- March 2019
- December 2018
- February 2018
- January 2018
- November 2017
- September 2017
- August 2017
- July 2017
- March 2017
- February 2017
- November 2016
- September 2016
- August 2016
- July 2016
- April 2016
- November 2015
- September 2015
- July 2015
- April 2015
- March 2015
- February 2015
Categories
- Actions
- Active Directory
- Analytics
- Architecture
- Bug fixing
- CDP/Personalize
- ChatGPT
- Content Edition Experience
- Content Hub
- Continuous Integration
- Dev
- Development
- Environments
- Experience Editor
- Experience Forms
- Front-end
- Health Check builds
- Helix
- How To
- LDAP
- MVP
- MVP Summit
- MVPinny
- Phantom JS
- Powershell
- QA
- Richtext Editor
- Rules
- Security Provider
- SIF
- Sitecore 9
- Sitecore API
- Sitecore Community
- SItecore Connect
- Sitecore Modules
- Sitecore Rocks
- Sitecore Rule Processor
- Sitecore Symposium
- SPE
- SPEAK
- SUG
- Support Ticket
- TDS
- Team City
- Uncategorized
- Upgrades
- Visual Studio
- WFFM
- Workflow
- XConnect
- xDB
- XM Cloud