Undefined script tag function

Tourist
38 0 1

Hello there,

I want to access logged in customer's email in a script tag. I've the following function in script tag js file.

function testfunction(email){
console.log(email);
}

I' preparing some customer content in this function and need to append it to the store footer. For this I added 

{% if customer %}
  <script>
  testfunction('{customer.email}')
  </script>
{% endif %}

in the footer.liquid theme. 
But when I run the store, I'm getting console error

Uncaught ReferenceError: testfunction is not defined

How can I get customer email in the script tag function?

0 Likes
Shopify Expert
665 170 199

Hi, @user072319 ,

This is Evita from On The Map.

testfunction() will not be found, because you added it script tag js file. It loads after liquid file is loaded.

Add testfunction like this inside script tag(<script>):

 

 

{% if customer %}
  <script>
function testfunction(email){
console.log(email);
} testfunction('{customer.email}') </script> {% endif %}

 

 

Best, Evita

On The Map Marketing | Developing custom Shopify Sites & Apps is our thing

- Install our latest app Accessibly - Makes your store accessible for everyone, helps to avoid fines
- Inc 5000 | Shopify Parners | 20+ stores launched | 300+ active clients
- Need help with your Shopify store? Reach out to us!
0 Likes
Highlighted
Tourist
38 0 1

@OTM Thank you for the response.
Is there any way I can access liquid variables inside script tag js file without manually editing theme files?

0 Likes