I am utilizing Bootstrap 5.3.0 and have included it through CDN links in my JS script. This is how I am injecting it using my js file within the existing website's DOM via a chrome extension named Scripty, which serves as a JS injector extension for chrome:
function loadFile(path, type) {
if (type == "js") {
var fileref = document.createElement("script");
fileref.setAttribute("type", "text/javascript");
fileref.setAttribute("src", path);
} else if (type == "css") {
var fileref = document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", path);
}
document.getElementsByTagName("head")[0].appendChild(fileref);
}
Then executing:
loadFile("https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="13717c7c67606761726353263d203d23">[email protected]</a>/dist/css/bootstrap.min.css","css");
loadFile("https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="77151818030403051607374259445947">[email protected]</a>/dist/js/bootstrap.bundle.min.js","js");
The loaded files are properly referenced in the header based on the browser dev tools inspection:
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcbeb3b3a8afa8aebdac9ce9f2eff2ec">[email protected]</a>/dist/css/bootstrap.min.css">
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="10727f7f64636462716050253e233e20">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
However, when attempting to create an object later in my js script, I encounter an error stating that the bootstrap class is not recognized. Here is the code snippet used to initialize the new instance of the class:
const bsContactMenu = new bootstrap.Offcanvas('#contactMenu');
This is the error highlighted in the browser's dev tools:
Uncaught ReferenceError: bootstrap is not defined
at AllInOne (<anonymous>:188:26)
at <anonymous>:631:17
Although inspecting the exact class in the developer Tools->Watch confirms its definition, I am puzzled as to why I am receiving an error indicating it is undefined.
Observations from Developer Tools
It is expected that the bootstrap.Offcanvas class should be correctly interpreted by the browser since the developer tools verify its presence in the DOM.