I have a cool bit of dojo running where I click a button and it brings a success message on the screen via JavaScript. Is it possible to issue a call to a server-side Groovy script (eg. foo.groovy) from within this JavaScript?
Not only do I want to show the cool success message, but I need to do some work in the background at that point also.
Here are some relevant links from the Dojo documentation:
Intro to AJAX and Dojo
Dojo.xhrGet() reference: sends a GET request to the server-side resource (which can be a Groovy script or anything else that resides on the server). The live examples on this page are currently broken, but I'm told this is being resolved.
Dojo.xhrPost() reference: sends a POST request. There are variants for PUT and DELETE too.
The answer was to use dojo's AJAX call system.
Related
I want to trigger an event directly from the Chrome developer tools.
I know that I can inspect an element and than reference to it using $0 in the console.
But how can I trigger a dom-event, on this element?
Yes you can. As a matter of fact you can try it right here on Stackoverflow.
Inspect your inbox. You will trigger a click event on that element.
Stackoverflow is convenient since it has jQuery already loaded so you can just do
$($0).trigger( "click");
And you will see the inbox behave as you would expect (open).
The command above uses jQuery $() to wrap the element $0 you have available.
With this you have all the beautiful functionality of jQuery.
In general you can use vanilla javascript for this too, but the commands for triggering events depend on the browser you are using.
You can of course load jQuery yourself via the command line
just copy the content of this paste it in the command line and press enter.
This will work great if the page you are viewing doesn't do anything that would cause any conflicts.
Moreover you can read this answer for vanilla javascript.
Considering that you're using chrome, you can use the URL-bar to simply type
javascript:document.getElementById("submit-button").form.submit()
to, in this case, submit an answer to StackOverflow. This way you can also trigger any other event, for example a url-click:
javascript:document.getElementById("answer-33569491").children[0].children[0].children[0].children[0].children[0].children[1].click()
This can also be entered in a watch, then you can execute it multiple times.
I have an HTML form that I want a user to fill out, and then send by pressing a button. Is there a way to have that button run a java program with all the HTML input as parameters? I looked into this a bit, and most people recommended servlets. Is there a way to do this without a servlet?
Edit: I'm not trying to use a servlet because of restrictions.
If you have a restriction against running a servlet you're pretty much out of luck in terms of using java. You could use a JSP, but that's just a servlet at the end of the day. You could use javascript to pop-up a mail-to with the form parameters entered as the body of the email but that would assume the user has a mail program configured. You could use some service like http://www.emailmeform.com/ (I have no idea if this one is good, I just googled it...).
Check out Java Web Start, might be what you're looking for.
Are you running the java client-side or server-side? The two posted answers are client-side answers. As for server-side, the tutorial here might help, depending on your server.
I have a program allowing script files to "compete" with each other in a games such as Tic Tac Toe .
The visualization is made in the console and looks something like this:
| XO |
| OX O|
| X |
- X is the winner! Meh..
Not very exciting.
I was thinking of making a Swing visualisation, but after seeing a presentation on the Raphaƫl JavaScript vector graphics library I decided that I'd be cool to make the visualization browser based, even though every thing will be running on the same computer.
Do I need a full fledged web server to accommodate for this? How does the JavaScript communicate with the Java program?
Building a Swing interface would be the most straightforward solution, but this would probably be the messiest one as well.
The web browser solution is probably the most satisfying if you already have a web server going, but has a lot of overhead to set up and properly understand. Then again, you have layers of different technology to play with and get confused in (java, JSP, HTML, javascript, css, etc).
These days, with HTML5 and available javascript libraries, the web interface is in my opinion the best choice for most interfaces, so you might as well set up your machine and have it available for the next project.
Any communication between the server (Java) and the browser will take place with a HTTP request from the browser.
This may happen in two ways:
a) By pressing a submit button on a browser HTML page and rendering a HTML page on the server as a response (usually through server side scripting like JSP, although you could generate the entire page through java code)
b) By using Ajax in javascript to make an asynchronous call to the server, which will respond with data that you can then interpret and render with javascript (probably the best solution for what you are trying to do). There are many ready javascript libraries to help you with this, including jQuery.
In method b, you would essentially be waiting on the server side for a post from the browser and then you would be responding with a page written in XML, json or even pure text. Your javascript code would then interpret the data and render it on the browser HTML page (which you will have loaded at the starting point for the application).
Do I need a full fledged web server to accommodate for this? How does the JavaScript communicate with the Java program?
If you're going to host the JavaScript and display in a browser, then yes, the browser would need to be able to request the data from the Java via an HTTP request. Although browsers can load data via file:/// URIs, it's (quite) awkward to handle those requests dynamically.
It's trivial to install a web server, though. Tomcat is easy to install, and there are several other options if you prefer, such as TJWS (no relation), Winstone, LiteWebServer, Jetty...
If you're forced to run Java for the game engine I would suggest using Jetty to give you the ability to service HTTP request from a browser. It's all embeddd in your application and there's no faffing with having to package your app as a WAR and deploy it everytime you make a change.
[edit]
Just read about Tiny Java Web Sever which could also be an alternative to Jetty if you need a smaller footprint.
Javascript can communicate with a Java applet on the page (at least in theory). A Java applet is going to throw up a warning when it loads, and requires that Java be installed on the machine.
Yes a webserver will be necessary. Javascript will probably run as part of an html form. If any Java side processing is required, then web server is necessary. Since it is not possible for the html/javascript to communicate with a standalone java program. But Why need a java server side program at all? Can the logic not be written in java script totally?
Suppose we have an applet on a webpage and JQuery code in the same page, as well. How can we notify JQuery upon an Applet action, for instance a timer in the applet stops and a file system change occurs, and i want to make some process upon this file change in JQuery part as soon as the change occurs. Any suggestions / solutions? Thanks.
You can start with short article: Java - Javascript interaction.
And then see Java-to-Javascript Communication and
JavaScript to Java Communication (Scripting) for more info.
I am working on an application in Linux which will interfaces with hardware. One of the requirements is to create the GUI in Web-browser . the application will be c++ based. I m not familiar with web realted stuff so i want to know Is it possible to do such a thing (currently it's a console application take input from txt file/cmd line). gui will be simple using button and showing output messages on browser from the application. i want to know which technologies/languages are involved and how can it be done. some of the idea i read but havn't found anything concrete yet. if u have any idea about these or a better suggestion please share
run the app in background and communicate with browser ?
call library functions directly from browser ?
any other idea ?
I would start by setting up a regular HTTP server, like lighttp or Apache httpd.
You say you already have a command line program that does the actual work - As a first step, I would reuse that, and configure the web server to call your program using CGI - see forexample http://httpd.apache.org/docs/2.2/howto/cgi.html for apache
Finally, I'd pick some javascript framework like jQuery or YUI with Ajax capabilities to do requests to the server to call the CGI script from within a webpage. You could also create a form-based web application without ajax or any framework, but that would require you to stuff all kinds of logic in your program to generate HTML pages. By using Ajax, you can leave the command line application as is, and parse any responses it gives with javascript, and then use that to dynamically change the webpage in a way that would make sense to the user.
If this all works, then I would try to figure out how to package all these components. Perhaps you just want to create a simple archive with all the programs inside, or maybe you want to go as far as actually embedding the webserver in your program. Alternatively, you may want to do it the other way around and rewrite your program as an ISAPI module that you can plug into your webserver. Or if that's not integrated enough still you could write your own (partial) HTTP server. That's really up to you (I'd probably spend time and energy on searching for the leanest, meanest existing open source http serverr and use that instead)
At any rate, the prior steps won't be lost work. Most likely, developing the web page is going form a substantial part of the work, so I would probably create a quick and dirty working solution first using the age-old CGI trick, and then develop the webpage to my satisfaction. At that point you can already have an acceptable distributable solution by simply putting all programs in a single archive (of course you would have to tweak the webserver's configuration too, like changing the default port so it won't interfere with existing webservers.) Only after that I would spend time on creating a more integrated fancy solution.
I ended up using Wt though I'd update for future reference.
These are how I thought of doing this, in order of complexity for me:
Create a simple server-side-language (PHP/Python) website that can communicate with (ie launch and process the return of) your application
Modify your application to have a built-in webserver that just punched out HTML (command line parameters taken through the URL)
Modify the app to publish JSON and use javascript on a simple HTML page to pull it in.
You could write a Java applet (as you've tagged this thread) but I think you'd be wasting time. This can be quite simple if you're willing to spend 10 minutes looking up a few simple commands.
After 12 years, web browser-based GUI started to appear, WebUI is one of them.