ReferenceError when trying to call functions

I am getting an invalid reference error whenever I try to call a javascript function on a separate lettercript tag. I have tried test code

HTML body:

<canvas id="c"></canvas>
<script type="text/javascript" src="vendor/paper.js"></script>
<script type="text/paperscript" src="foo1.js" canvas="c"></script>
<script type="text/javascript" src="foo2.js"></script>

      

Canvas will not be used in this example, it is just that paper.js should work.

So, for the scripts themselves:

foo1.js (papercript)

function blah(){ console.log("blah"); }

      

foo2.js (javascript)

window.onload = function{ blah(); }

      

Now if I load the HTML, the console tells me that blah () is throwing a link error. However, if I changed

type="text/paperscript"

      

to

type="text/javascript"

      

for foo1.js then the console will display "blah" and no error will be thrown. Why is this?

+3


source to share


1 answer


Browsers will only load scripts with type = "text / javascript". A document library can have a script loader with type = "text / document" initiated from a window. Loading. Try adding an event listener so you don't overwrite the existing loader.

Example:



function myInit() {
 blah();
}

if (window.addEventListener) {
  window.addEventListener('load', myInit, false); 
} else if (window.attachEvent) {
  window.attachEvent('onload', myInit);
}

      

0


source







All Articles