Script run on server or client? - javascript

I have a thttpd server set-up which has the following html file. When I give address server-address/file-name.html on a standard web browser errors on the script is logged in error console of browser. I am confused about where the script is run actually? Is it on the client side or are the error messages just passed on to browser by server?
My requirement is to run script on a server to generate dynamic web pages upon client interaction.
<html>
<head>
<title>Entitled Document</title>
<script language="JavaScript" >
Function Java_Scriptfn()
{
alert('Test'
}
</script>
</head>
<body>
<input type="button" value="Script_Check" onclick="Java_Scriptfn()">
</body>
</html>

That is purely client side code, so it runs on the client.
As far as I can tell, thttpd only supports server side programming via CGI.

JavaScript that is embedded in a HTML site (either inline or load from another file) is always executed client-side (that means in your browser).
If you want it to be executed, server-side, you need something like node.js.

It's client side code; any Javascript files included in an HTML page will run client-side (although they can talk to a server, that's different).

Related

What did I just implement with Node.js?

I'm following this tutorial, I was confused at the point where it says:
"...
Surprisingly the code is very simple:"
// Connect to the socket.io server
var socket = io.connect('http://localhost:8080');
// Wait for data from the server
socket.on('output', function (data) {
...
I'm not sure where to put this code. I tried to add it to browser JS, like this:
<html>
<head>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script> <!-- here -->
// Connect to the socket.io server
var socket = io.connect('http://localhost:8080');
// ...
</script>
</head>
<body>
<h1>SSH</h1>
<div class="terminal"></div>
</body>
</html>
And it worked! Is this correct? I thought it was meant to be a server-side code.
Anyways, now I get a terminal which i can interact with. But I'm not sure what it is doing. I was trying to implement an SSH client, but it looks like I obtained an on-browser terminal, over which I will manually connect to SSH?
Also I believe this would only work on the local machine. But what I want is a -remote- web server that can access to my machine using SSH (although it may not be very safe). Am I in the right direction? How can I implement a web server that acts as a client to the SSH server on my machine?
Thanks,
It has absolutely NOTHING to do with SSH in any way, shape or form.
It's a websocket server/client, which allows you to send messages(not commands) between a browser and a server.
It's most commonly used for chat applications, although there are endless other uses.
However, with this mechanism in place, you could interpret certain messages on the server and make them execute the commands you wish to allow your users to use.
Quick example of how it would work (server side) :
socket.on('ls',(path,cb)=>{
fs.readdir(path, (err, files) => {
cb(files);
});
});
and on the client :
socket.emit('ls','/home',(files)=>{
console.log(files);
};
The client here emits a 'ls' event, with a path (user selected or something); and the server interprets this message, get the list of files for the given path, and returns it to the client. This mechanism could be used to implement a variety of commands. But keep in mind that this is NOT SSH.
Read more on Socket.io
If you are following the tutorial the server side code is server.js. This is a simple express.js webserver with a socket.io extension.
The code in the .html file is send to the browser which acts as client.
That's a socket server. It listens for connections from the browser. That's what you're doing in the HTML.

Python in HTML Code

I have a Python file, called function.py, which contains this code:
def double(x):
return x * 2
I also have this HTML code:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
alert(double(4));
</script>
</body>
</html>
Is there a way for me to load the Python file in HTML of JavaScript, or is that impossible?
No. You cannot execute python directly in your web page. The web page is in the browser and the browser does not know how to execute python.
So, the python needs to run on the server. You have a couple options for how you can access the python on the server from your web page:
You can use a template system that allows you to insert python generated results in the web page BEFORE it is sent to the browser so when it gets to the browser, your python code has already been run and the results were placed into the web page for your page Javascript to access in the browser.
You can make an Ajax call with Javascript from your web page to your server and you can code your python server to respond to that Ajax call and return results. The Javascript in the web page can then do whatever it wants to with the results of the Ajax call.

How to run another program from HTML

I have the following snippet of the code:
<html>
<head>
<title>Example of Very First PHP Script ever!</title>
</head>
<body>
<script>
function comexe(){
C:\Program Files\Notepad++\notepad++.exe C:\Documents and Settings\User\Desktop\file.txt
}
</script>
music
</body>
</html>
How can I run another program from the HTML? What am I doing wrong?
Adding this as an answer, as requested in the comments
You can't - HTML is client side, and javascript is sandboxed to protect the client PC from exactly this. Apart from the title, your script has nothing to do with PHP - which can run a filesystem command, but only on the server.
Technically you could execute a program from inline vbscript. You would normally do this in a .hta file running locally.
Something like:
<html>
<head>
<title>Example of Very First PHP Script ever!</title>
</head>
<body>
<script language="VBScript">
Sub RunProgram
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "C:\Program Files\Notepad++\notepad++.exe C:\Documents and Settings\User\Desktop\file.txt"
End Sub
</script>
music
</body>
</html>
You can not do that becasue html and javascript both client side tech. and have restriction so that it can not access resource of the client machine.
If it's a operation that you have to perform many times on client machine this could be achieved.
Just install a web server (wamp or xamp or what you want on machine) and from your page call
http://localhost/nameofscript.php
this script can execute whatever you want if you set correct permissions on the user of local webserver
Perhaps, if is one-time operation that a client must execute is not so pratic to install a webserver only for an onetime operation
Bertoli Stefano

ServerSide JavaScript

I have a question about SSJS.
With SSJS, is it possible to hide code from the user?
Other ServerSide languages like PHP aren't viewable in the source because they are processed before the client side, the browser
A little example of what I want:
<html>
<head>
<script runat="server">
function getPassword(){
var password = "myPass";
return password;
}
</script>
</head>
<body>
<script>
alert(getPassword());
</script>
</body>
</html>
I tested this, but the password is still viewable
Am I doing something wrong so that my example is simple CSJS or is it impossible to hide SSJS-code?
Adhering to the wise words of T.J. Crowder, your file could be a classic .asp file on a windows based host (so a file with the extension .asp), looking like this:
<% #language=ecmascript %>
<%
function getPassword(){
var password = "myPass";
return password;
}
%>
<html>
<head>
</head>
<body>
<script>
alert('<%=getPassword()%>');
</script>
</body>
</html>
Or more in line with your style it could look like:
<script language="jscript" runat="server">
// mind the 'language' property, it is required.
// The script tag doesn't have to be
// in the header of the html-document.
function getPassword(){
var password = "myPass";
return password;
}
</script>
<html>
<head>
</head>
<body>
<script>
alert('<%=getPassword()%>');
</script>
</body>
</html>
Both ways the server side scripting will not be visible in the page source. Aside: <%=...%> can be viewed as shorthand for Response.Write(...)
For other hosts, check your provider or the wikipedia list given in David Dorwards answer
Yes, your server-side code can be hidden from the user, exactly as with any other server-side language. You have to serve the relevant HTML file via a server that understands server-side JavaScript, and you have to configure that server correctly (by default, .html files probably aren't going to be pre-processed; typically HTML files with server-side code would have a different extension, such as .shtml, .asp, .aspx, etc., depending on the platform on which you're running them; although of course with proper configuration you can have anything handled correctly). If you were able to see the code above via a web browser, then you've missed out one of those steps.
Note that server-side JavaScript will have the benefits, and disadvantages, of any other server-side language. You can't (for instance) do any scripting of the client's browser with any server-side language, which is why you see so much client-side code around. (Perhaps that was obvious. :-) )
Any server side JavaScript will only be available on the server but, just as you need to run PHP through a PHP engine before sending it to the client, you have to run SSJS through a JS engine before sending it on.
You can find a list of such engines at Wikipedia, although node.js is probably the most popular at present.
runat="server" is, IIRC, an ASP.NET construct. If you want to go down that route, you'll need to be using ASP.NET for a start. I've no idea if the syntax you have is right though.

HTML script element localhost uri

This may seem like a silly question, but consider the following use of the 'script' element to import an external javascript file:
<script src="http://localhost:8085/myscript.js" type="text/javascript" >
Given that this is a url and not a file path, would this be evaluated server side or client side?
If evaluated client side, then it would fail as the script resides on the server, not the 'localhost' of the client?
Thanks.
Client side, and yes - respectively.
The myscript.js file is downloaded from the server (localhost:8085) then executed by the browser on the client-side.
Javascript is always executed on the client-side.

Categories

Resources