I am trying to trigger an event in JavaScript using a Java class.
How can I send javascript code (either direct input or from a file) in a Java class to a specified destination file which is already opened in the browser (Firefox)?
Currently I'm using the ScriptEngineManager to run my JavaScript code, but the code is executed in my Java environment and I don't know how to trigger an event in this way (as I cannot use e.g. window.postMessage("hi", "*");)
Any suggestions how to solve this? Work-arounds are also appreciated (preferably without extensions, plugins, ...).
Best regards.
When executing from java you have no access to window events - because there is no browser.
I don't really understand the use case here but you could load the destination file into an iframe and then do a meta refresh - once you update the files contents then the updated results will be displayed on the next refesh.
In any case you can only return results from the inputted javascript functions back to java - not call events.
You could pass a javascript function (a call that simulates the event) back to the browser and then execute it.
How you would pass this back to the client depends on the situation :
It could be on the response of the request to upload the javascript
You could use the meta-refresh mechanism
You could look into an ajax style way with client polling (if you didn't want to refresh manually each time).
EDIT
The server has no link to the browser between requests. To asynchronously receive a server event in a browser you could have a look at:
WebSockets
Comet
So the browser would receive notification when the task is complete and then it would be able to react - for instance prompt for the tab to close.
Related
I am newbie on Java Spring and I am trying to get the event that is working on a web button. What is the correct way to do this in terms of debug a listener or an event? I mean, I want to find the code that is executed when the user push one button. Is any way to do this using Eclipse or IntelliJ ?
Thanks in advance!
Emma.
A button on a webpage cannot directly call java code as the webpage is displayed on the client and java only runs on the server.
The way client and server generally interact is via requests. For example the client sends a get request and receives a webpage to display.
Generally the way the client sends information back to the server is via forms sending post requests to the server which get handled by spring's #PostMapping methods which is where the java code first starts to execute.
If you simply want a button that does something on the client, without interacting with the server then you want to use Javascript with an onClick event.
I am working on java web application. In my application some user defined javascript using.
When i update my javascript file and run my project in browsers, so first time not get updated javascript file and when i refresh (ctrl+F5) page after get updated javascript file.
so why first time i don't get updated javascript file?
Please guide me.
You need to adjust the cache headers that your server is sending. Specifically, you need to lower the expiration date that clients are likely to use for things like Javascript files.
Otherwise, the client is going to cache them -- just like they are supposed to do.
My requirement is to automate configuring an instance(instance of my company product I am working on). The scenario is such that in the middle of my configuration, the control goes from window to browser and the rest of the configuration process is handled in the browser and so the entire process is a combination of window-based and browser-based. I have used AutoIT to complete the window-based configuration and when the control goes to the browser, I am using _FFStart() $ffUrl = _FF_GetCurrentURL(). I am getting the URL in a variable in AutoIT script. I want to automate the further web-based configuration through Selenium. I am calling this AutoIT compiled script from my Java class.
My question is that is there a way in which I can get the URL that I am saving in my AutoIT script so that I can create a web driver instance, open the browser with the URL and further handle my configuration process using Selenium.
if you start/run an exe file from your code you can path variable as argument
something like:
$va = "my variable"
run("myexe.exe $var)
or equvivalent
or generally:
You can share variables between appliations/processes many ways
using windows messaging look up _WinAPI_PostMessage function that sends message the specific window or broadcast message to all windows so all running app gets the message (2 variables can also be passed) in autoit you can set up a function that runs (stoppping the main program) when your autoit app receives such message and variables
you can set up UDP or TCP channels so that your running apps can communicate (UDP should work fine) one app being the server the other is the client
file communication setup, where the variable or information to be sent is written to a file and the other app reads the file (it can be good to combine with the first method: App 'A' writes data to file 'data.msg' then sends message to App 'B' that upon receiving the message reads 'data.msg') you can establsih intricate protocol as well with Acknowladge messages etc. in case your application requires it.
_WinAPI_RegisterWindowMessage
_WinAPI_PostMessage
GUIRegisterMsg
are the relevant functions you can look them up in autoit help
If you think any of these is feasible but you need further assistance I can write a simple code that demonstrates that in practice
I have a regular JSP/Servlet/Java web application that is used for uploading pictures from a mobile device. I am using Apache Commons library for the upload. Application is hosted on WebSphere Application Server 7.0.
Everything is working fine and the user can upload several images totaling 8MB or more if he has a really good/strong signal/connection or on a good WiFi.
The problem arises when the user is at a location with poor 3G/4G signal/connection. He gets errors like "Illegal state exception" or some time-out error, and in some cases the mobile browser just stays on the submit page with the progress bar no longer moving.
Any suggestions on how to "gracefully" handle this? Like is there a way to intervene after a set amount of time and give the the user an option to submit the form without the file attachment (i.e. just submit the form text fields)? Any other suggestions are welcome too.
UPDATE: The setTimeout solution below worked for me. The other missing piece was that I have to issue a "browser stop" command to stop the original submission that's in progress before I can issue a re-submit. Otherwise, my re-submit command will just be ignored by the browser.
The usecase here is simple - if the upload didn't finish in N minutes, remove/clear the field using javascript and resend the form.
You don't need to control the upload in the basic implementation, just safely asume that if you set a timeout to resend, it won't happen if the first attempt was successful and the page reloaded.
jQuery pseudocode:
setTimeout(function(){
$imageFieldNode.remove();
$form.trigger('submit');
},30000);//after 30 seconds
The more advanced way is to use a ready solution for controlled upload. They work like that:
upload starts
js prompts the server in intervals with a GET query to get the size of content that was already received.
everytime it gets the info - it reports progress.
You can do a lot with these libs.
You can think about the approach used in popular webmail clients (when attaching files to a message):
The files are uploaded independently (i.e. before) of the form submit, using javascript. Each of the files are stored in a temporary directory, and after the upload succeeds the user can proceed with the action.
The upload status is displayed to the user, and if it fails the main action (form fill/submit) does not get interrupted.
I am preparing a program in java and its purpose is to make the HttpWebRequest for a url which can open in any browser (i am planning to use watij for opening the page in different browser).Now my program should take the screen shot of the presently opened url once the page has loaded successfully.I need to run a piece of code after the browser has loaded successfully in the browser.I dont want to use the javascript document.ready function for identifying if the page is loaded successfully or not.I should be able to know the page load complete status in the server side (s0 that i can execute that piece of code for taking the screenshot).How can i do it in java.Or is there any other way to do achieve that if its not possible in java.May be create some plugins.
An early reply is highly valued,
Regards,Sagar.
you can do it using applet, [just taking snapshot i meant that you want.]