SP.ClientContext is not defined but all libraries are loaded

Before the question I want to give some background as I feel the problem here could stem from anywhere. On our SharePoint site, we have a page with a button that an html file stored in the sites document library. That file has a JavaScript file associated with it also stored in the document library. Together the files provide users with a data entry interface. Ideally, once the user submits the form, the JavaScript creates a new item in one of the lists on the SharePoint site. Below is a test snippet of the code that runs after submission.

function createItemInSharePointList(survey) {
    try {
        // Ensure SP.ClientContext is defined
        if (typeof SP === 'undefined' || typeof SP.ClientContext === 'undefined') {
            console.error("SharePoint scripts not loaded.");
            return;
        }

        // Get the current SharePoint context
        var context = SP.ClientContext.get_current();

        // Get the target list
        var list = context.get_web().get_lists().getByTitle("Test List");

        // Create a new list item
        var itemCreateInfo = new SP.ListItemCreationInformation();
        var newItem = list.addItem(itemCreateInfo);

        // Set the field values for the new item
        newItem.set_item("Item 1", survey.data.firstQ);
        newItem.set_item("Item 2", survey.data.secondQ);
        newItem.set_item("Item 3", survey.data.thirdQ);

        // Save the changes
        newItem.update();
        context.load(newItem);

        // Execute the request
        context.executeQueryAsync(
            function () {
                console.log("Item created successfully!");
            },
            function (sender, args) {
                console.error("Error creating item: " + args.get_message());
            }
        );
    } catch (error) {
        console.error("Error creating item in SharePoint list:", error);
    }
}

Now for the problem. When the user submits the form, an error is thrown that “SP.ClientContext is not defined.” Initially I thought the issue was that the SP.js library wasn’t loaded, but after placing some checks in the html that doesn’t appear to be the case. The code below prints that everything has been loaded successfully except SP.ClientContext.

<!-- Add references to the SharePoint JSOM libraries -->
<script type="text/javascript" src="<the-sharepoint-site-goes-here>/_layouts/15/sp.runtime.js" onload="console.log('sp.runtime.js loaded successfully');" onerror="console.error('Failed to load sp.runtime.js');"></script>
<script type="text/javascript" src="<the-sharepoint-site-goes-here>/_layouts/15/sp.js" onload="console.log('sp.js loaded successfully');" onerror="console.error('Failed to load sp.js');"></script>

<script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function() {
        console.log('Document fully loaded and parsed');

        if (typeof SP !== 'undefined' && SP.ClientContext) {
            console.log('SP.ClientContext is defined');
        } else {
            console.error('SP.ClientContext is not defined');
        }
    });
</script>

This code was created by a coworker and not myself, so as a result I’m not certain the way it has been done is even correct. I’m not sure if this makes a difference but the SharePoint site is on-prem. Any guidance for how to fix this would be great.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật