javascript - 如何使用从 HTML 文件到 js 文件的 JS 变量?
问题描述
我正在使用 express.js 开发一个网站,我想知道如何将 javascript 中的变量从 ejs 文件导出到 js 文件。
ejs 文件:
<div class="select">
<select id="selection_channel">
<%
let channelText = guild.channels.cache.filter(channel => channel.type == 'text');
channelText.forEach((channel)=>{
%>
<option value="<% channel.id %>">#<%= channel.name %></option>
<% }); %>
<p></p>
<script type="text/javascript" src="../js/submit.js"></script>
</select>
</div>
我想将 strUser 变量导出到我的 js 文件夹。
我的 js 文件:
// Settings endpoint.
app.post("/dashboard/:guildID", checkAuth, async (req, res) => {
// We validate the request, check if guild exists, member is in guild and if member has minimum permissions, if not, we redirect it back.
const guild = client.guilds.cache.get(req.params.guildID);
if (!guild) return res.redirect("/dashboard");
const member = guild.members.cache.get(req.user.id);
if (!member) return res.redirect("/dashboard");
if (!member.permissions.has("MANAGE_GUILD")) return res.redirect("/dashboard");
// We retrive the settings stored for this guild.
var storedSettings = await GuildSettings.findOne({ gid: guild.id });
if (!storedSettings) {
// If there are no settings stored for this guild, we create them and try to retrive them again.
const newSettings = new GuildSettings({
gid: guild.id
});
await newSettings.save().catch(()=>{});
storedSettings = await GuildSettings.findOne({ gid: guild.id });
}
//var strUser = require('./templates/settings.ejs')
// We set the prefix of the server settings to the one that was sent in request from the form.
storedSettings.prefix = req.strUser //req.body.prefix
console.log(req.body.strUser)
// We save the settings.
await storedSettings.save().catch(() => {});
// We render the template with an alert text which confirms that settings have been saved.
renderTemplate(res, req, "settings.ejs", { guild, settings: storedSettings, alert: "Changement effectué avec succès." });
});
解决方案
您可以将其附加到窗口对象
window.usrString = userString;
推荐阅读
- javascript - 使用类型柱形图在 highchart.js 中循环数据
- grid - UFT 15.01 读取桌面应用程序网格表的时间过长
- angular - 如何修复 Angular 12 中的 Ngx-custom-validator?
- imagemagick - ImageMagick - 将图像的白色转换为另一种,保持透明度并排除黑色
- java - 如何在二维数组上向后添加元素
- r - R ggplot2图例显示顺序
- python - Apache Kafka Kerberos 使用 Python 进行身份验证
- lda - 关于“潜在狄利克雷分配的吉布斯采样器推导”的问题
- linux - Azure Linux WebApp 网络核心日志记录到磁盘工作示例
- python-3.x - 如何在我的自定义 python 包中包含自动下载?