admin管理员组文章数量:1277900
I have this ejs file which is to be send as a response using res.render() in node.js
<!DOCTYPE html>
<html>
<head>
<title>pilcit</title>
</head>
<body>
<% var btn = document.getElementById('btn');%>
<% btn.style.background="blue"; %>
<div class="pt-5">
<div class="container-fluid">
<h1 style="color:blue;" align=center>Pilcit</h2>
<h4 style="color:#369bf4" align="center">The online clipboard</h4>
<div>
<textarea class="form-control mt-5" name="content" rows="12">
<%= result.content %>
</textarea>
<input type="text" value="sdfdsfsdfsd" id="foo">
<button id="btn" data-clipboard-target="#foo" copy clip </button>
</div>
<!--<input type="submit" value="Create Clip">-->
</div>
but the line
`<% var btn = document.getElementById('btn');%>
gives me this error
ReferenceError: /home/ubuntu/workspace/pilcit/views/show.ejs:34
32| </head>
33| <body>
>> 34| <% var btn = document.getElementById('btn');%>
35| <div class="pt-5">
36|
37| <div class="container-fluid">
document is not defined
at eval (eval at pile (/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:618:12), <anonymous>:17:27)
at returnedFn (/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:653:17)
at tryHandleCache (/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:251:36)
at View.exports.renderFile [as engine] (/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:482:10)
at View.render (/home/ubuntu/workspace/pilcit/node_modules/express/lib/view.js:135:8)
at tryRender (/home/ubuntu/workspace/pilcit/node_modules/express
I'm not using the <% var btn = document.getElementById('btn');%> to change the background color of the btn, I want to use it with clipboard js. I used that line to check whether the embedded javascript is working.
I have this ejs file which is to be send as a response using res.render() in node.js
<!DOCTYPE html>
<html>
<head>
<title>pilcit</title>
</head>
<body>
<% var btn = document.getElementById('btn');%>
<% btn.style.background="blue"; %>
<div class="pt-5">
<div class="container-fluid">
<h1 style="color:blue;" align=center>Pilcit</h2>
<h4 style="color:#369bf4" align="center">The online clipboard</h4>
<div>
<textarea class="form-control mt-5" name="content" rows="12">
<%= result.content %>
</textarea>
<input type="text" value="sdfdsfsdfsd" id="foo">
<button id="btn" data-clipboard-target="#foo" copy clip </button>
</div>
<!--<input type="submit" value="Create Clip">-->
</div>
but the line
`<% var btn = document.getElementById('btn');%>
gives me this error
ReferenceError: /home/ubuntu/workspace/pilcit/views/show.ejs:34
32| </head>
33| <body>
>> 34| <% var btn = document.getElementById('btn');%>
35| <div class="pt-5">
36|
37| <div class="container-fluid">
document is not defined
at eval (eval at pile (/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:618:12), <anonymous>:17:27)
at returnedFn (/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:653:17)
at tryHandleCache (/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:251:36)
at View.exports.renderFile [as engine] (/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:482:10)
at View.render (/home/ubuntu/workspace/pilcit/node_modules/express/lib/view.js:135:8)
at tryRender (/home/ubuntu/workspace/pilcit/node_modules/express
I'm not using the <% var btn = document.getElementById('btn');%> to change the background color of the btn, I want to use it with clipboard js. I used that line to check whether the embedded javascript is working.
Share Improve this question asked Jan 3, 2019 at 15:56 Adarsh DAdarsh D 5817 silver badges16 bronze badges 1-
2
The order is:
<% ejs code %>
is used on the server to build HTML -> the resulting document is sent to the client / browser -> the browser parses the HTML and builds the DOM ->document
begins to exist – user5734311 Commented Jan 3, 2019 at 16:09
1 Answer
Reset to default 10You can't use document
inside your ejs tags because that code is executed on the server. Instead you should add a script tag which will run as soon as the page is actually loaded in the browser.
<script> var btn = document.getElementById('btn'); </script>
本文标签:
版权声明:本文标题:javascript - "document is not defined " error in ejs file which is to be used with res.render() in node.js? - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741301838a2371132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论