Can't include a JavaScript library


#1

Hi, I am having trouble with including a JavaScript library in an .html file. The file begins like this (note comment!):

<!DOCTYPE html>
<html>
<head>
	<title>GamePad Library Demo</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
	<script src="name.js"></script>
<script>
     // IF I COPY THE CONTENTS OF name.js HERE, EVERYTHING WORKS PERFECTLY!   
</script>
</head>

The file name.js is in the same folder as the .html file… am I doing something wrong?

Have been playing around with it for hours, can anybody help me?


#2

Not sure if this will work but try adding a slash (/) before src="name.js". Or create a folder called js and change the src to src="js/name.js".


#3

Thanks for the suggestions! I just tried both, still doesn’t work though…


#4

Try including type="text/javascript" before the src attribute.


#5

Yea, tried that before, and tried again just now… doesn’t do it…

I am working on a sort of offline version of cloud9, (on a BeagleBone Black running Debian, the cloud9 ide comes with it.) Still should work though!

Just pasting the 1000+ line library right into my HTML file wouldn’t be a good idea I think…


#6

Any other suggestions? Maybe some sort of work-around I could try?


#7

use chrome devtools to see what script browser tries to load when you use src=name.js
the above can only work if your html file and name.js are in same folder, and your server doesn’t use some special rules.
otherwise you need to use a relative path instead.


#9

Hi, thanks for the reply! I’ll try to explain in more detail what seems to be happening, I hope it makes sense, I am somewhat new to server programming…

This is my server:

var app = require('http').createServer(handler);
var fs = require('fs');

app.listen(8888);
console.log('Server running on: http://' + getIPAddress() + ':8888');
function handler (req, res) {
  if (req.url == "/favicon.ico"){   // handle requests for favico.ico
  res.writeHead(200, {'Content-Type': 'image/x-icon'} );
  res.end();
  console.log('favicon requested');
  return;
  }
  fs.readFile('try2.html',    // load html file
  
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }
    res.writeHead(200);
    res.end(data);
  });
}
 

// Get server IP address on LAN
function getIPAddress() {
  var interfaces = require('os').networkInterfaces();
  for (var devName in interfaces) {
    var iface = interfaces[devName];
    for (var i = 0; i < iface.length; i++) {
      var alias = iface[i];
      if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
        return alias.address;
    }
  }
  return '0.0.0.0';
}

try2.html is the page I included an excerpt from in my original post; when I run the server I can access it at http://192.168.7.2:8888/. You said to use the Chrome devtools to see what script my browser tries to load… If I’m understanding the Chrome devtools correctly, my browser tries to find name.js at http://192.168.7.2:8888/name.js, and then somehow “fills” name.js with the contents of try2.html… is this possible???

You suggested using a relative path, how would I do that?
Or could I alter my server to somehow give the page access to name.js???


#10

sorry about the duplicate posts, don’t know how that happened… :frowning:


#11

Well, to get name.js on the browser, you need to send it from server when requested.
see https://github.com/ajaxorg/ace/blob/master/static.js#L29 for a simple example of static file server.
In the final application you probably should use one of existing modules from npm (e.g. connect) instead of writing completely custom one


#12

Thanks for the tip… I tried copying parts from the link you sent me… In the end, I went with this server: https://gist.github.com/ryanflorence/701407 It seems to work perfectly for my application, so far at least. I have started to look at npm connect, might switch to that at some point… Thanks again, John