EJS文件Express中未定义的导航器 [英] Navigator undefined in ejs file express
本文介绍了EJS文件Express中未定义的导航器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取用户位置的纬度和经度.在ejs文件中,我得到的错误导航器未定义.有人可以帮忙吗?谢谢!
I want to get the latitude and longitude of the user's location. In the ejs file I am getting error navigator undefined.Can somebody please help?Thanks in advance.
<%if ('geolocation' in navigator) { console.log("geolocation supported"); } %>
<%else { console.log("Geolocation is not supported by this browser."); }%>
更新的代码:
<html>
<head>
<meta charset="utf-8">
<title>Daily Journal</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/styles.css">
</head>
<body>
<p>Testing</p>
<div class="container">
</div>
<div class="footer-padding"></div>
<div class="footer">
<p>Made with ❤️ by The App Brewery</p>
</div>
<script>
if ('geolocation' in navigator) {
console.log("geolocation supported");
} else {
console.log("Geolocation is not supported by this browser.");
}
</script>
</body>
</html>
```
Error:
C:\Users\HP\Desktop\API-App\ejs-challenge\public\weather.js:2
if ('geolocation' in navigator) {
ReferenceError: navigator is not defined
at C:\Users\HP\Desktop\API-App\ejs-challenge\public\weather.js:2:24
at Object.<anonymous> (C:\Users\HP\Desktop\API-App\ejs-challenge\public\weather.js:9:3)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (C:\Users\HP\Desktop\API-App\ejs-challenge\app.js:6:15)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
推荐答案
ejs
-模板在您的节点服务器上呈现,其中没有对 navigator
的引用(或 window
).但是,您可以做的是将上述代码包含在< script>
-标签中,并将其返回到模板中:
The ejs
-template is rendered on your node-server, where no reference to navigator
(or window
for that matter) exists. What you can do, however, is to include above code in a <script>
-tag and return this in your template:
// your-template.ejs
<h1>Hello EJS World </h1>
<script>
if ('geolocation' in navigator) {
console.log("geolocation supported");
} else {
console.log("Geolocation is not supported by this browser.");
}
</script>
这篇关于EJS文件Express中未定义的导航器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文