Javascript js 脚本可以获取在同一文件中的 EJS 上下文/页面中写入的变量吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28603658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can a js script get a variable written in a EJS context/page within the same file
提问by Donovant
As I wrote in the title, I'd like to get a value from a variable written into a ejs page/file, from a javascript file within the same page
正如我在标题中写的,我想从写入 ejs 页面/文件的变量中获取值,从同一页面内的 javascript 文件
EJS:
EJS:
<% var test = 101; %>
JS:
JS:
<script>
var getTest = test;
</script>
Or what if I'd like to use a function (with parameter) written into a EJS file and use this function in a JS context where the parameter is given to the function from JS context
或者,如果我想使用写入 EJS 文件的函数(带参数)并在 JS 上下文中使用此函数,其中参数从 JS 上下文提供给函数,该怎么办
EJS:
EJS:
<% function fn(par){ ... } %>
JS:
JS:
<script>
var test = 101;
<%>fn(test)<%>
</script>
回答by Naeem Shaikh
Edit: this Half considers you are using EJS on serverside
编辑:这一半认为您在服务器端使用 EJS
1) You can pass an ejs variable value to a Javascript variable
1) 您可以将 ejs 变量值传递给 Javascript 变量
<% var test = 101; %> // variable created by ejs
<script>
var getTest = <%= test %>; //var test is now assigned to getTest which will only work on browsers
console.log(getTest); // successfully prints 101 on browser
</script>
simply create an ejs variable and assign the value inside the script tag to the var getTest
只需创建一个 ejs 变量并将脚本标记内的值分配给 var getTest
Ex: var getTest = <%= test %>;
前任: var getTest = <%= test %>;
2) You can'tpass an javascript variable value to a ejs variable
2)不能将 javascript 变量值传递给 ejs 变量
Yes, you cant: if it is on server.
是的,你不能:如果它在服务器上。
Why:
为什么:
The EJS template will be rendered on the server before the Javscript is started execution(it will start on browser), so there is no way going back to server and ask for some previous changes on the page which is already sent to the browser.
EJS 模板将在 Javscript 开始执行之前呈现在服务器上(它将在浏览器上启动),因此无法返回服务器并要求对已发送到浏览器的页面进行一些先前的更改。
Edit编辑:这一半认为您正在使用 EJSClient客户端
3) if EJS is on client side, and pass EJS variable to javascript
3) 如果 EJS 在客户端,并将 EJS 变量传递给 javascript
The answer above will still work, but you will require to load the script within the EJS template, not the script loaded before the template rendered(in that case it will of-course no be a valid javascript).
上面的答案仍然有效,但您需要在 EJS 模板中加载脚本,而不是在模板呈现之前加载的脚本(在这种情况下,它当然不是有效的 javascript)。
4) if EJS is on client side, and pass javascript variable to EJS
4) 如果 EJS 在客户端,并将 javascript 变量传递给 EJS
I m sorry I have myself not tried this case, but I really look forward if someone answers this case
很抱歉我自己没有尝试过这个案例,但我真的很期待有人回答这个案例
回答by Hugo
The above answer didn't work for me. You can use a div this way:
上面的答案对我不起作用。您可以这样使用 div:
<div id="mydiv" data-test=<%= test %>></div>
And access the data variable 'test' that you gave it in a script tag:
并访问您在脚本标记中提供的数据变量“test”:
<script>var test = document.getElementById('mydiv').dataset.test</script>
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset
回答by we_mor
Inside your <script>,
在你的里面<script>,
you can create a divelement:
你可以创建一个div元素:
const div = document.createElement('div');
and give it an innerTextvalue like:
并给它一个innerText值,如:
div.innerText = `<%= data_from_your_server_response %>`
If you console.log(div), the data from your server response will be displayed.
如果您console.log(div),将显示来自您的服务器响应的数据。

