0

I'm trying to run the Socket.io example program offline. The index.html page calls the Jquery library like so:

<script src="http://code.jquery.com/jquery-1.11.1.js"></script>

However, this obviously won't load without an internet connection. I have tried using a file in the same folder as the rest of the project:

 <script src="jquery-1.11.1.js"></script>

But I get a 404 error in the developer menu when running that as well. I'm not sure if I'm putting the file in the wrong spot, or if I'm using the script tag incorrectly, Any help is appreciated.

EDIT: Here is the full index.html file:

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="/socket.io/socket.io.js"></script>
    <script src="jquery.js"></script>
    <script>
      var socket = io();
      $('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
      });
    </script>
  </body>
</html>

Picture of Error Message

Picture of index.js

  • Will show you were it is trying to get it from..what is different than where it should be looking? – charlietfl Jun 01 '16 at 23:27
  • Please provide a screenshot of the directory structure of your project. Also include which folder the jquery.js file is located in. This will allow us to tell you the exact path to use in your – Matthew Jun 02 '16 at 16:14

7 Answers7

0

Ok, I recommend changing your index.js to server.js, it makes more sense that is more like a server file for your app. It will serve a lot more things not just the index page. However, you can name it whatever you want. :)

Your problem is that node does not know your file system. You send the index back with __dirname +/index.html. __dirname tells node the current directory but on your index file, script src on your index.html file does not know where to start.

On your index.js file, tell node where to get all the static files, between line 3 and 5 app this.

app.use('/s', express.static(__dirname + '/static'));

the '/s' is the alias for this static directory that we're going to define. __dirname is your root dirctory and '/static' is the folder where i want all my static files to be since i don't want to expose my whole root.

For that exact line to work, you will need to add a folder name "static" on the directory where your index.js and index.html is right now.

on your index.html file, instead of

<script type="text/javascript" language="javascript" src="jquery.js">

change the src to "/s/jquery.js" since node.js now knows what alias s represents.

<script type="text/javascript" language="/s/javascript" src="jquery.js">

good luck

chungtinhlakho
  • 870
  • 10
  • 21
0

If your Html and JS file are both in the same folder it should work definitely. Otherwise you need to put an address relative to your current file. Say your JS file is in the parent folder of your HTML file then :

<script src="../jquery-1.11.1.js"></script>
akardon
  • 43,164
  • 4
  • 34
  • 42
0

Maybe this question can help you with local local resources references How to properly reference local resources in HTML?

Community
  • 1
  • 1
Fábio Luiz
  • 438
  • 2
  • 11
0

If your file is in the same directory as your html the following should make it work :

<script src="jquery-1.10.2.js"></script>

If it's in a folder that is in the same directory as your HTML use:

<script src="FOLDERNAME/jquery-1.10.2.js"></script>

If it's a level above your HTML file use:

<script src="../jquery-1.10.2.js"></script>

If none of these work be sure to double check your file's name in case there's a space in beginning or the end of the file's name.

[EDIT]

Well, if there's no spelling error and the change of placement doesn't fix it, then probably one or both of the files are corrupted. By the way, your jQuery version is outdated. Download v2.1.1 or use this script tag:

<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Angel Politis
  • 10,955
  • 14
  • 48
  • 66
0

Don't forget you can access the jquery file by typing in the exact (local) URL into your browser. Test by loading the URL http://localhost:[port#]/jquery-1.11.1.js Alter this URL with the correct directory path for your dev environment until your jquery successfully loads.

Matthew
  • 123
  • 1
  • 9
0

Paul try followings:

Verify jquery file name and path if socket.io.js using jQuery change the order like

<script src="jquery.js"></script>
<script src="/socket.io/socket.io.js"></script>

call javascript code after page load i.e. $(function(){}) as below:

$(function(){
var socket = io();
  $('form').submit(function(){
    socket.emit('chat message', $('#m').val());
    $('#m').val('');
    return false;
  });
  socket.on('chat message', function(msg){
    $('#messages').append($('<li>').text(msg));
  });
})

Thanks

Sami
  • 3,686
  • 4
  • 17
  • 28
0

I had the same problem and I was looking for the solution around. I found your question but I didn`t find the answer so I posted my own question here. Someone actually knew how to make it work. So if you still want to know just check the answer I got: Raspberry Pi - Flask Server using JQuery not working Offline (Online it works)

Community
  • 1
  • 1