Javascript Get Page Source (server-side) - javascript

Is it possible to get the source of a server-side page from a javascript script? I want to be able to get the source of a page that is in the same folder on the server. Is it possible without using anything other than javascript?

If you want to get the result of the execution of some server side script (often HTML) from javascript you could use AJAX.
If you want to get the source code of some server side script using javascript (such as the C#, Java, PHP, ... code), you can forget about it unless you publish this source code as text file so that the server doesn't try to interpret it and then use AJAX to fetch this text file from javascript. Of course anyone will be able to access it, not only javascript hosted on your site.

Related

How to run PHP script actively on client side / client machine?

I'm trying to run a PHP script directly on the client side rather than taking request from a client to server & receiving back processed output or file. I created a PHP script which is running properly on the server side. I want to run that same PHP script on the client side as well. The PHP is not installed on the client side, it is only on the server side. Using <object> method I somehow managed to execute a PHP script on the client side. But it's just window or small frame that is visible on the current browser or in a page on the browser of a local machine (or client machine). So whenever PHP functions are invoked, it is executed at the server side, not on the client side.
Is there a way to execute PHP functions actually on the client side as well?
OR
Is there a way to open that entire working PHP page on the client side rather than on small window or in a frame of server page on a client browser without PHP installation?
What I tried is:
I created PHP script as "SERVER.php" which displays the name of all the files of a current working directory of the SERVER machine.
<?php
exec('dir', $status, $result);
var_dump($status);
?>
I want to get name of all the files of a current working directory but of CLIENT machine.
I created a html file as "CLIENT.html" that consists of the following code:
<object type="text/html" data="http://192.168.0.110:81/file2v2.php" width="800px" height="600px" style="overflow:auto;border:5px ridge blue">
</object>
Basically, the object function used in html contains the ip address followed by the port no. (of the Apache Server) and the php file name of the server (where php is installed and located).
Can anyone please help me how to execute PHP functions or say entire PHP scripts on the client side, or the way it can get it done.
All clients where you want to execute PHP scripts need to have PHP installed, as well as a running webserver (if those scripts are accessed via http). There is no way around that.
One way to do it without client side code is to give your webserver SSH access to all the clients. (This means creating the user on each machine and giving it the necessary permissions). Then the server could read the visitor's IP, connect to it via SSH and read the filesystem of the client and display it in the browser. This would be a wild approach, but possible.
Check this out: https://kvz.io/blog/2007/07/24/make-ssh-connections-with-php/
It would also mean that if someone gets access to that server or can somehow exploit it, all clients are compromised as well.
To run a script, you need an interpreter installed, the browser can read js script, but cannot read php script, for example.
https://en.wikipedia.org/wiki/PHP
I think you should take a look at the difference between client-side and server-side. Take a look here, it explains a lot.
Quote from the above link:
Key Differences Between Server-side Scripting and Client-side Scripting
Server-side scripting is used at the backend, where the source code is not viewable or hidden at the client side (browser). On the other hand, client-side scripting is used at the front end which users can see from the browser.
When a server-side script is processed it communicates to the server. As against, client-side scripting does not need any server interaction.
The client-side scripting language involves languages such as HTML, CSS and JavaScript. In contrast, programming languages such as PHP, ASP.net, Ruby, ColdFusion, Python, C#, Java, C++, etc.
Server-side scripting is useful in customizing the web pages and implement the dynamic changes in the websites. Conversely, the client-side script can effectively minimize the load to the server.
Server-side scripting is more secure than client-side scripting as the server side scripts are usually hidden from the client end, while a client-side script is visible to the users.
If you want every logic done on the client-side, take a look at Angular, VueJS or just basic Javascript. But with Javascript (including Angular/VueJS/Etc.) you can not manipulate client-side files, which would be a major security issue if it was able to. To change things on the server-side, you will always need a back-end programming language like PHP, C#, Java etc.
Be aware that everything you do on the client-side, is visible for the client and can be changed by the client. Which makes your application vulnerable for attackers.
To answer your question: You can't.
Basically, no.
But you can read about C# Blazor approach (https://dotnet.microsoft.com/apps/aspnet/web-apps/blazor) and try to implement something like this.

Execute server side ruby file using javascript

I have a ruby file on my server (a simple test file). I am wondering how I can use javascript to execute that file on server with a button click. Nothing fancy I want here like rails or asp or php. Its just a small test and I want to see how far it goes. On terminal, all I have to do is ruby test.rb. Is there a way to do this with javascript on an html page?
Adapt the script so your webserver can access it with a Rack or CGI.
Then you just need to make an HTTP request to it. You could set location = URI, call submit() on a form, use XMLHttpRequest or any of a host of other options.
Well browsers don't run Ruby, so I think you need to send a request with that button click to your server, and then the server will run the file.
Try Sinatra: http://www.sinatrarb.com/

Run program on tomcat server with Javascript, is it possible?

so, here is the thing, I need to run .jar I've programmed myself on a server.
On the otherside, that server is also a webserver, so it displays webpages and so. Those webpages are written in simple HTML and JavaScript.
So, here is the thing:
is there a way to run my program when users perform click on links in the webpage?
I created a function in Javascript, but I cannot make it work, as long as I don't know how to run shell commands in Javascript.
The execution of the program is totally transparent to the user, and what it basically does is to search content in some documents in order to update the html that is shown to the user.
Any ideas? Hope I made myself clear.
Thanks in advance!
You really can't run shell commands in JavaScript.
If I understand your question correctly, you're looking to do some sort of remote process execution.
Is your JAR file on the other server - is it running as part of a web server, I.E. as a servlet in an application container like Tomcat, such that your code can be executed by calling HTTP methods? (If not, you may want to start out with doing this.) If so, then you'll need to have your JavaScript make the HTTP call to the server to execute its code. A common way of doing this today is through the use of AJAX - and you can use something like jQuery to help with this. The response from this AJAX request (could be XML, JSON, or pre-formatted HTML) could contain the details to "update the HTML that is shown to the user".
The simplest way to do this is put the html, javascript, and a jsp page in the same war file, and then run it on tomcat. Not the best way, but definitely the quickest.
EDIT:
So to clarify, the JSP page will get invoked by the click (ajax call, use jquery), and renders some json/xml. The success handler for the ajax call takes the jsp response and updates your html page. So the jsp runs on the server, but the html/javascript runs in the browser.
If you want to make if fancier, you can use spring web-mvc and write a controller instead of a jsp page. It has the same overall effect either way.

With JavaScript is it possible to Read/Write from/to a file on the server

I have a series of JSON Objects I want to save locally on my server. I am attempting to avoid any server-side script like PHP as required per demand of whats being built. I know its a security risk, but that in this case is not a particular worry. So that said is it possible to actually write to a file and or get its contents via javascript with or without the help of a lib such as jquery?
I should mention I am attempting to avoid ActiveX as I know this is an IE only feature and the software we are developing is planned to be Cross Browser supported
So that said is it possible to actually write to a file and or get its contents via javascript with or without the help of a lib such as jquery?
Nope. You will need something running on server side that can receive your JavaScript input and write it to the server.
Internet Explorer's proprietary file writing functionality is for writing local (client-side) files only.
You can read a file using ajax, but without a server side language you cannot write a file to the server.
https://developer.mozilla.org/en/ajax
No. Javascript runs on the client. You need server-side code to access the server's file system.
Client-side JavaScript can only send data to a server, there's no way for it to tell the server what to do with the data.
To save data to a file or db on a server, you'll require a server-side script of some sort (could be server-side JS with Node.js). If all you need is persistent data, you could store some JSON strings in localStorage or in cookies as needed. They wouldn't be shareable that way though.
Yes, you can use AJAX requests in JavaScript without using jQuery. However, jQuery will save you an ungodly amount of time and cross-browser testing.
But, as others have already said, you can't write server files without server code.

Run cgi script without changing the current html page?

I have a cgi script written in C, which is actually a client program.
What I need to do is when the up arrow key is pressed, I need the cgi program to execute and send data to a C server program residing in my webserver?
How can I exectute cgi scripts without the page changing ?
my C server program is listening on port 5000,
my html page is in http server at port 80. On loading, when the user presses the up arrow key,
the cgi script must run and send a value 1 to the server program. Also my cgi script is a client program.
I have already written the Javascript code to take keyboard inputs and displays particular values.
Running a CGI script is just a matter of making an HTTP request. There are numerous ways to do this without leaving the page.
Some of the more common ones are:
Load the URL in an (i)frame
Have the script return an image and dynamically generate an <img> element
Use XMLHttpRequest to make the request
Have the script return a piece of JS and dynamically generate an <script> element
Most of these come under the general heading of Ajax (and XHR being the most well associated method). There are plenty of libraries that help with this, and most big libraries (such as YUI and jQuery) include Ajax helper methods.
You want to communicate with the server without having to reload the page? Use AJAX (javascript framework).

Categories

Resources