[英]Can Node.js write javascript to the browser along with html?
Here's what I would think works, but doesn't:这是我认为有效的方法,但没有:
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>' +
'<script type="text/javascript">alert("hi")</script>'
);
What ends up happening is the browser will write the script tag, but doesn't evaluate it as javascript.最终发生的是浏览器将写入脚本标签,但不会将其评估为 javascript。 Can this work, or do I need to send the javascript in its own res.writeHead() prior to the html?
这可以工作吗,还是我需要在 html 之前在自己的 res.writeHead() 中发送 javascript?
Edit: the actual html that gets written is just编辑:实际写入的 html 只是
<form action="/upload" enctype="multipart/form-data" method="post"><input name="title" type="text"><br><input name="upload" multiple="multiple" type="file"><br><input value="Upload" type="submit"></form>
<script type="text/javascript">alert("hi")</script>
Thanks谢谢
Works just fine for me.对我来说效果很好。 I am using expressjs to simplify my work
我正在使用expressjs来简化我的工作
var express = require('express'),
app = express.createServer();
app.get('/', function(req, res){
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>' +
'<script type="text/javascript">alert("hi")</script>'
);
});
app.listen(3000, '127.0.0.1');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.