简体   繁体   中英

How do you link and use an external JS file and use it in ejs files?

I'm trying to link a javascript file to an ejs file. It contains code for drawing a randomised canvas background.

I have this in my app file:

app.use('/js', express.static(__dirname + 'public/js'));

I have tried linking it like this in the ejs(html):

<script src="js/bg.js"></script>

And tried to use it's function init(no variables) like this:

<canvas id="canvas" width="7735" height="4354" onload="init()"></canvas>

It works perfectly if i put the code into this:

<script> code... </script>

So my problem is probably linking incorrectly. I didn't get any errors in visual code or firefox :( Can someone help me with linking an external js file?

It was actually correct. The problem was that the onload was on the wrong element. Opps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM