Search code examples
javascriptmobilewebsquarespace

Squarespace website not loading on mobile when a redirect Javascript code is added


I'm running a squarespace website for a friend. The issue is that he's got a desktop specific homepage and he wants mobile user's to be directed to a different, existing page. Now I thought this would be straight forward as I've done it on other websites using the following javascript code:

<script type="text/javascript">
<!--
if (screen.width <= 699) {
window.location = "https://www.thehealthytray.com/home";
}
//-->
</script>

That's the correct URL in the script. I injected this code into the 'Code injection' section in squarespace. For some reason, when trying to load the website on a mobile, it will look like it's going to load and the link successfully changes in the url box. However, it never loads and eventually gives a cannot display message.

I'm a bit stuck with it and would appreciate any help!

Cheers


Solution

  • You put the code in the wrong spot. It is on every page of the site so you are stuck in an endless loop. Home > Mobile Home > Mobile Home ... Mobile Home ... keeps reloading. You have to add it at the page level. Go into the page settings (the little cog next to the page in the left side bar). Go to "Advanced" and enter the code into the "Page Header Code Injection" section.