Asked by Alexandre Smadja on Aug 11, 2024
Alexandre wants to dynamically change the content of a schema.org FAQPage `<script>` tag based on keywords in the page URL. His attempt to use HubL `{% set %}` variables inside a JavaScript `if` statement (checking `window.location`) isn't working as expected.
Answered by Matt
Matt explains that HubL runs on the server before the page is sent to the browser, while JavaScript runs in the browser. Therefore, JS variables cannot control HubL `set` statements in this way. The solution is to perform the conditional logic entirely in HubL using the page URL variable (`page_meta.canonical_url`) and HubL's `string_containing` test.