I’m getting this error, which references a line number (ln 77) in the dom library, so my guess is that my html found the dom file, but I can’t figure out why that file can’t find p5. I currently have p5.js in a different folder from p5.dom.js; could this be the problem?
The error message:
TypeError: p5 is undefined p5.dom.js:77:3
My code works fine if I remove any (very few) references to dom elements, but I’d like to add a slider and ideally access the rest of the dom related stuff, so please help!
UPDATE
What I’m actually trying to do is load my scripts dynamically. I want to pick a random js file and run that as my script. That works fine for just p5js but for some reason it breaks when I try to load the dom this way too. Linking these files statically works fine like this:
<script src="../js/p5js/p5.js" type="text/javascript"></script>
<script src="../js/p5js/addons/p5.dom.js" type="text/javascript"></script>
<script src="../js/script.js" type="text/javascript"></script>
But I want to be able to vary the path to my js file arbitrarily (like a random choice from a hardcoded array of path strings). This seems to cause one of two problems. Here’s what I do with just p5 and my script:
<script>function initjs(){
// load p5js
var p5js = document.createElement("script");
p5js.src = "../js/p5js/p5.js";
p5js.type = "text/javascript";
document.getElementsByTagName('head')[0].appendChild(p5js);
// load script
var script = document.createElement("script");
script.src = "../js/" + jsfiles[Math.floor(Math.random()*jsfiles.length)];
script.type = "text/javascript";
document.getElementsByTagName('head')[0].appendChild(script);
}</script>
...
<body onload="initjs();">
And this works just fine. The problem occurs when I try to add the dom to this.
<script>function initjs(){
// load p5js
...
// load p5domjs
var dom = document.createElement("script");
dom.src = "../js/p5js/addons/p5.dom.js";
dom.type = "text/javascript";
document.getElementsByTagName('head')[0].appendChild(dom);
// load script
...
}</script>
...
<body onload="initjs();">
This seems like it would work because the other combination worked, but it fails giving the error from above. My assumption is that it is taking too long for either the dom or p5 to load and therefore not all variables could be initialized. Taking a look at line 77 in p5.dom.js, which gave this error:
TypeError: p5 is undefined p5.dom.js:77:3
The dom is trying to reference a variable `p5’ but I don’t make that so I assume that is part of the p5.js file. Anyway, in order to fix this I added a couple timeouts to try to give them enough time to initialize and the errors went away:
function loadp5() {
// load p5js
var p5js = document.createElement("script");
p5js.onload = function(){setTimeout(loaddom, 50);}; <!--- added these --->
p5js.src = "../js/p5js/p5.js";
p5js.type = "text/javascript";
document.getElementsByTagName('head')[0].appendChild(p5js);
}
function loaddom() {
// load p5domjs
var dom = document.createElement("script");
dom.onload = function(){setTimeout(loadscript,50);}; <!--- added these --->
dom.src = "../js/p5js/addons/p5.dom.js";
dom.type = "text/javascript";
document.getElementsByTagName('head')[0].appendChild(dom);
}
function loadscript() {
// load a random js file
var script = document.createElement("script");
script.src = "../js/" + jsfiles[Math.floor(Math.random()*jsfiles.length)];
script.type = "text/javascript";
document.getElementsByTagName('head')[0].appendChild(script);
}
...
<body onload="loadp5();">
As I said, the errors in the console went away but now my script is never called! I put console.log calls in the setup() and draw() functions of script.js and in loadp5(), loaddom(), and loadscript(). Only the logs from the load functions appear so my script is not getting program control for some reason. I’m sure I’m doing something wrong, so please help! and yes I know I can just do the static loading and use a single js file for my script but that would lead to ugly code because I want to have a ton of options for the random script.
THANKS