重定向Javascript时无法从同一目录加载资源 [英] Failed to load resource from same directory when redirecting Javascript

查看:89
本文介绍了重定向Javascript时无法从同一目录加载资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在同一个目录下有两个文件,但每当我打电话时:

window.location.href =./page.html\";window.location.href =/page.html ;



从我的index.html中我得到一个加载失败资源错误。

谢谢!

解决方案

要使用Express提供静态文件,您应该使用 express.static 或者你必须为每个html文件定义一个新的路由,或者重新创建由 express.static 提供的功能。 (如果你不想使用Express,那么请参阅

$ b

h3> app.js

  var path = require('path' ); 
var express = require('express');
var app = express();

var htmlPath = path.join(__ dirname,'html');

app.use(express.static(htmlPath));

var server = app.listen(3000,function(){
var host ='localhost';
var port = server.address()。port;
console.log('在http://'+ host +'上侦听:'+ port +'/');
});

将文件放入 html 子目录中。例如:

html / index.html



 <!doctype html> 
< html lang =en>
< head>
< meta charset =utf-8>
< title> index.html< / title>
< / head>
< body>
< h1> index.html< / h1>
< p> 2秒重定向...< / p>
< script>
setTimeout(function(){
window.location.href =./page.html;
},2000);
< / script>
< / body>
< / html>



html / page.html



 <!doctype html> 
< html lang =en>
< head>
< meta charset =utf-8>
< title> page.html< / title>
< / head>
< body>
< h1> page.html< / h1>
< p> 2秒重定向...< / p>
< script>
setTimeout(function(){
window.location.href =./index.html;
},2000);
< / script>
< / body>
< / html>

这些文件会每2秒重定向。



你可以从GitHub上下载这个例子:



更多示例可以使用和不使用Express:



其他相关答案:


I am writing an application in NodeJS.

I have two files in the same directory but whenever I call either:

window.location.href = "./page.html";


window.location.href = "/page.html";

from my index.html I get a failed to load resource error.

Thanks!

解决方案

To serve static files with Express, you should use express.static or otherwise you will have to define a new route for every html file that you have, or reinvent the functionality provided by express.static. (If you don't want to use Express for that then see this answer.)

You can do something like this:

app.js

var path = require('path');
var express = require('express');
var app = express();

var htmlPath = path.join(__dirname, 'html');

app.use(express.static(htmlPath));

var server = app.listen(3000, function () {
    var host = 'localhost';
    var port = server.address().port;
    console.log('listening on http://'+host+':'+port+'/');
});

Put your files in the html subdirectory. For example:

html/index.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>index.html</title>
</head>
<body>
<h1>index.html</h1>
<p>Redirection in 2s...</p>
<script>
setTimeout(function () {
   window.location.href = "./page.html";
}, 2000);
</script>
</body>
</html>

html/page.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>page.html</title>
</head>
<body>
<h1>page.html</h1>
<p>Redirection in 2s...</p>
<script>
setTimeout(function () {
   window.location.href = "./index.html";
}, 2000);
</script>
</body>
</html>

And the files will redirect every 2 seconds.

You can download this example from GitHub:

More examples to do the same with and without Express:

Other related answers:

这篇关于重定向Javascript时无法从同一目录加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆