Run cgi script without changing the current html page? - javascript

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).

Related

Run a script in a google cloud from a local html page

I would like to run a script in a google cloud server using a local HTML page.
To be more clear the steps would be:
open a local HTML page on my local computer.
push a button that triggers a script in my google cloud server.
the script creates a file in the server that I can download pressing
another button.
I'm new in this field and I don't know where to start.
How do I connect to the server via HTML? (PHP?, Javascript?)
How does the authorization process work?
There are several languages and strategies that you can use.
You can use locally Javascript or PHP (it needs installation and configuration) that will allow you, for example, to make an HTTP request (it may also be another protocol), to a script (that can be in PHP or Javascript or others) on the server, which, upon receiving the request, processes and generates a file for a specific path.
Then on that other button you make a request to that path to download the file.
My suggestion is to choose the languages and implement with these to understand the process.
Create an HTML page, put a button.
Attach a function to button onClick to send an Ajax request
That would cause cross server request challenge for you down the road..
You can simply put a URL from your local web page styled as a button to your Google Cloud hosted application.
Create the file on the server side and you can set an HTML header
Content-Disposition: attachment; filename="results.csv"
to make a file downloaded to the user end.

Can PHP run after the page is loaded?

So PHP is executed server-side. But is it possible for PHP to be run after the page is loaded?
To illustrate, if I had a value (stored in a file, lets say) that changed every second. When I hit a button that uses Javascript to ask PHP to get that value, does it display what the value currently is, or what it was at page load?
I think you need to get one of those diagrams that show how basic HTTP and the web server works. It will make more sense to you, than explained in plain words here.
In the simplest possible case, the result of you typing some address and getting a web page with its contents can be summed up, due to a result of process in request/response relationship between your browser and a web server located somewhere in the world.
Plain HTML
In a less simpler way, think of it like this. basically, if a page is during a refresh phase, (meaning you clicked something and are waiting for a data to comeback) then, that means it is getting/loading the response from the web server. If the web server does not have PHP installed as a module, then the only thing it is waiting/loading (in many cases) is plain HTML content.
With PHP
On the other hand, if we assume you have a file called index.php in your webserver, and have PHP is installed, in this case the web server will send everything that appears in-between <?php ?> to the PHP interpreter, first, then wait for it until PHP does its magic and send back to the server only the result.
<?php
echo 1+1;
?>
So, in the above case, the webserver (ex: Apache, Nginx) does not care what is inside the opening and closing tags, and sends the entire code to the PHP interpreter, and PHP would compute that script according the way it understands it and sends only the computed result back to the server, as plain HTML. In this case the number 2.
The role of AJAX.
AJAX (Asynchronous JavaScript and XML) is a technique used Javascript, to help you send requests and receive the response without having to load the page. This is usually done by using the browsers XHR object. So, there is no mystery in this whole shebang.
The above can be summed up simply in the following steps.
Enter foo.com browser sends a request to the server of foo.com
server/browser exchange messages server allows browser to aquire
information server sends index.php back to browser if <?php tag
is found in the script, server sends all the codes inclosed in those
tags to the PHP interpreter The PHP interpreter, compiles the query
and sends the result as HTMl
PHP is server-side script, before return page content to client side like browser, it should parse all the PHP logic into HTML logic, so it should display the value which was at page load. And if your PHP logic consumes long time to execute, it will delay the content display at client side.
Your example case is really difficult to explain, without getting technical, or using a lot of chained logic...
...however, I'll try to keep this a little more simple:
The overwhelmingly vast majority of the time, PHP will run only when something connects to the server, and will stop running as soon as it's done running the script that was accessed.
That "something" might be a page-load (//mysite/index.php), or it might be issuing an XMLHttpRequest ("AJAX") to ask the server for data (//mysite/articles.json).
Not all languages work this way.

Best Practice: Call .Net Function after webpage is visible for the user

I'm in search of some best practices. I have a webpage asp.net (a loading page as you will).
Once this page is loaded (and visible to the user) i want to execute a function that might last some seconds.
problem is, how do i launch this function?
I've tried loadComplete (event),unload (event) , working with timeout (javascript to poll itself with json) and even the timer-class (.Net). Could anyone point out an alternative/ best pratices to perform this kind of action?
This won't work the way that you are trying to do it. To understand why you have to understand the concept of webservers:
Webservers use some code (PHP, ASP.NET, ...) to generate dynamical HTML code that is beeing sent to the client. This process works in the following steps (in case of ASP.NET)
Client requests something from the server.
Server generates html (that's your ASP.NET / C# code)
Server sends HTML to client
The browser on the client receives the HTML and displays it.
So your ASP.NET code is only running in step 2. This makes it impossible to catch the event when the client has rendered his page in his browser. However there is client-side technology called AJAX. You can make AJAX calls using javascript (that runs in the browser of the client).
So for example you can create a javascript function that sends a second request to the server when the html was loaded in the client, and executes some function on the server.
For this you will need a webservice on the server that will serve as a interface between the javascript and your C# code.
A easy way to create webservices is to use WCF webservices
If you have any further questions feel free to comment on this answer.

Javascript Get Page Source (server-side)

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.

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.

Categories

Resources