简体   繁体   English

链接到外部js文件不起作用

[英]Link to external js file not working <script src=“client.js”

I've been playing with nodejs, using websockets to communicate between server and browser. 我一直在玩nodejs,使用websocket在服务器和浏览器之间进行通信。 This is my working client code: 这是我工作的客户代码:

<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
<script>
 var socket = io();
 socket.on('ida',function(data){
   console.log(data.news);
   socket.emit('vuelta',{news: 'answer'});
 });
</script>

Then why if I put 那我为什么放

var socket = io();
 socket.on('ida',function(data){
   console.log(data.news);
   socket.emit('vuelta',{news: 'answer'});
 });

in a separated client.js file and link it like 在单独的client.js文件中,并将其链接为

<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
<script src="client.js" type="text/javascript"></script>

it just doesn't work? 就是行不通吗? In the browser's console, instead of showing data.news it says: Uncaught SyntaxError: Unexpected token < in client.js:1 在浏览器的控制台中,它没有显示data.news,而是显示:Uncaught SyntaxError:client.js:1中的意外令牌<

It might be that you are not serving the file at all. 可能是您根本没有提供文件。 Be careful to include it in the http server configuration, on your node server file. 小心将其包括在节点服务器文件的http服务器配置中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM