Jump to content

Executing my javascript code only on a specific page

Recommended Posts

Hello there, 

I have written a javascript with some jQuery to call an api and get some data to be rendered when a user clicks on a specific page. I have put this code inside the header of code injection section. But the problem is the script seems to execute only when user access the site the first time, not when they are accessing that specific page.

Can anyone be able to help me out to have a workaround to this problem?

 

Edited by yasmikash
Link to comment

Here's what you need to do. 

First, wrap your code as follows

<script>
window.Squarespace.onInitialize(Y, function(){

// Your code here

});
</script>

 

This will execute the code on each page load. 

Next, you may need to modify the code so that it only runs for the specific page. You do this by testing the body tag for the presence of the specific collection id. 

Link to comment
20 hours ago, colin.irwin said:

Here's what you need to do. 

First, wrap your code as follows


<script>
window.Squarespace.onInitialize(Y, function(){

// Your code here

});
</script>

 

This will execute the code on each page load. 

Next, you may need to modify the code so that it only runs for the specific page. You do this by testing the body tag for the presence of the specific collection id. 

Thanks! This really helped me!!

Link to comment
  • 1 year later...
On 11/7/2019 at 7:01 PM, colin.irwin said:

Here's what you need to do. 

First, wrap your code as follows

<script>
window.Squarespace.onInitialize(Y, function(){

// Your code here

});
</script>

 

This will execute the code on each page load. 

Next, you may need to modify the code so that it only runs for the specific page. You do this by testing the body tag for the presence of the specific collection id. 

Hi @colin.irwin, I am wondering if this code works for my case? I am trying to trigger the script on the product-varian select (or shall we say combobox) provided by template.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.