0

some.liquid

<div>this is {{year}}</div>

<script>
var year = ''
if (location.hostname == 'stackoverflow.com') {
  year = 'good'
} else {
  year = 'nood'
}

</script>

Obviously, the idea above cannot be implemented, so what should I do?

1 Answer 1

2

The reason why it can't work that way is that Shopify doesn't execute JavaScript on their server when parsing liquid file. So you want to manually modify that element using JavaScript

<div id="my-element"></div>

<script>
const myElement = document.getElementById('my-element');

var year = ''
if (location.hostname == 'stackoverflow.com') {
  year = 'good'
} else {
  year = 'nood'
}

myElement.innerText = `This is ${year}`

</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I have been troubled by this problem for a long time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.