Originally Posted by
Lesshardtofind;
Code:
function getParameterByName(name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
I thought since you are requiring the results[1] spot to be the parameter value, it might also be a good idea to guard against the user passing in "(", ")" too. Although I don't know for sure that these would even be valid in the name? That's a pretty cool idea for a module .
Originally Posted by
MutantJohn;
I also learned there's server-side JavaScript too. But without DOM, what's the point? Does anyone here have any good experience using SSJS and it being a better choice than other popular server-side scripting languages?
It's more than just server scripting though. Node.js I think is an attempt to give JavaScript some of the lower level access that other languages have, but to maintain the asynchronous nature that the browser exhibits. For instance you can do file I/O and none of it will be blocking. This model would seem to give the best performance "per thread", but Node.js is single threaded so you wouldn't want to do any huge processing of any data during a callback without spawning a new process to do it (something I'm currently not that good at handling).
In my Chatroom/Canvas program, all the server really needs to do on any request/callback is to assign a few variables and then sometimes broadcast those to all the clients. So it handles it fast enough that for a small number of clients that it appears as if they can all draw things all at once.
To give a small example of a very simple file server written in Node.js (From the book "Eloquent JavaScript" I think):
Code:
var http = require("http"), // http module
fs = require("fs"), // file-system module
path = require("path"), // Contains a parser for filepaths
mime = require("mime"), // This is an npm module, that contains a JSON based mime-type lookup table
cache = {}, // Will cache away filedata
defaultPath = "./public",
defaultFile = "/hello.html",
server;
function send(response, statusCode, body, mime) {
response.setHeader("Content-Type", mime || "text/plain");
response.setHeader("Content-Length", Buffer.byteLength(body));
response.writeHead(statusCode);
response.end(body); // End the connection and send the information
}
function sendFile(response, filePath) {
if (cache[filePath]) {
send(response, 200, cache[filePath], mime.lookup(path.basename(filePath)));
} else {
// Normally you would add some checking here to see if 'filePath' is valid,
// and also to root out some possible attacks, this is just a brief thing though.
// Notice how we pass a function to fs.readFile, so that we continue execution directly
// after the call, even though the file data isn't read yet.
fs.readFile(filePath, function (error, data) {
if (!error) {
cache[filePath] = data;
send(response, 200, data, mime.lookup(path.basename(filePath)));
} else {
send(response, 500, "Server Error.");
}
});
}
}
// In creating our server we also register our callback that is executed on the "request" event.
// The 'request' parameter is a 'readable stream' so you could repond to data as it's read if
// you wanted to (again with a callback).
// The 'response' object is a 'writeable stream' object, and you could pipe file data to it
// as it's read if you wanted.
server = http.createServer(function (request, response) {
// Very minimal basic routing:
var filePath = defaultPath + (request.url === "/" ? defaultFile : request.url);
sendFile(response, filePath);
});
server.listen(8000);
Edit: The 'require' functions in the example are part of a pattern called 'Common Modules'. It's basically a synchronous request to a .js file that will assign things to the global 'module.exports' variable when it is evaluated. There is another pattern called AMD (Asynchronous Module Definition) that could possibly be used as well, but Node.js uses this one.
Originally Posted by
Phantomotap;
I just realized that I will have written my first HTML twenty years ago in just two more months.
I feel so fogy just now...
Soma
Mine started about 15 years ago. Everything is so different now as to be unrecognizable. I just remember before I let off, everyone was sure XHTML would take over, and MIME was getting a ton of hype .