I am trying to understand why my script tag is not being executed with EJS. I am using Express JS and EJS template engine. I am sending to front end a compiled EJS partial template:
router.get("/piechart/", async (req, res, next) => {
try {
const ejsPartialFile = await readFileAsync(
"./piechart-title.ejs",
{ encoding: "utf-8" }
);
const data = {title: "something important"}
let ejsFunction = ejs.compile(ejsPartialFile, { client: true });
res.status(200).send(ejsFunction ({ data }));
} catch (e) {
console.error(e);
next(e);
}
});
Which also contains tag with javascript file:
<div class="form-group">
<label>Edit Piechart title</label>
<input
type="text"
class="form-control"
name="piechart-title"
value="<%= data.title %>"
maxlength="10"
/>
</div>
<script src="/javascripts/testing.js"></script>
The javascript just contains some random stuff which should be executed when it is read:
(function() {
console.log("testing if javascript file is being read");
})();
However, what happens is that the html gets added with correct things, to the page, however, the javascript doesnt get executed and I get no error. I guess something blocks it? Maybe someone has an idea?
Regards, Rokas
UPDATE
I attach javascript which is used to attach the html:
if (response.status === 200) {
const data = await response.text();
const element = document.getElementById("edit-fields");
onePagerEditField.innerHTML = data;
}