Jump to content

Read simple text file with javascript

Recommended Posts

Posted (edited)

I uploaded a simple text file to my Squarespace site (Business Plan subscription, 1 year auto-renewal). I injected javascript into the footer to test code to read the contents of the file (see below). The output in the Dev console (Firefox) is not the contents of the file I expected, but rather - Records: <empty string>. I've used this technique successfully on Wordpress sites. Any idea why this isn't working on Squarespace?

UPDATE: Looking deeper, is it possible the responseText is empty because Squarespace doesn't put 'Access-Control-Allow-Origin' in the request's response header? If so, is there another approach I can use to accomplish my goal?

<script type="text/javascript">

rawFile = new XMLHttpRequest();
rawFile.open("GET", 'https://lily-pike-4bpy.squarespace.com/s/motd.txt', false);

    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                var allText = rawFile.responseText;
                console.log('Records:',allText);
            }
        }
    }
    rawFile.send(null);
</script>

 

Edited by RKN
Updated info
  • 2 weeks later...
  • Replies 1
  • Views 413
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted
On 8/27/2022 at 7:51 AM, RKN said:

I uploaded a simple text file to my Squarespace site (Business Plan subscription, 1 year auto-renewal). I injected javascript into the footer to test code to read the contents of the file (see below). The output in the Dev console (Firefox) is not the contents of the file I expected, but rather - Records: <empty string>. I've used this technique successfully on Wordpress sites. Any idea why this isn't working on Squarespace?

UPDATE: Looking deeper, is it possible the responseText is empty because Squarespace doesn't put 'Access-Control-Allow-Origin' in the request's response header? If so, is there another approach I can use to accomplish my goal?

<script type="text/javascript">

rawFile = new XMLHttpRequest();
rawFile.open("GET", 'https://lily-pike-4bpy.squarespace.com/s/motd.txt', false);

    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                var allText = rawFile.responseText;
                console.log('Records:',allText);
            }
        }
    }
    rawFile.send(null);
</script>

 

Can you share your site with the protected password so I can take a look?

BeyondSpace - Squarespace Website Developer

🖼️ Lightbox Studio (Pinch/Zoom images, videos - PDFs Lightbox - ...) </>  🗓️ Delivery Date Picker (Date picker form field)
Gallery block 7.1 workaround </> 🤖 Ask me anything

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.