How do I submit a form in jsoup that has a JavaScript function for onclick? There is no submit button, just an anchor tag with button styling.
Example:
<a tabindex="3" onclick="return login();" href="javascript:return login();" onkeypress="hitLogin(event)">Login</a>
How do I call a JavaScript function with jsoup. Is that even possible? Should I look into some libraries like Node.js or PhantomJS?
The HTML entity parameter onclick takes a string of Javascript that it will run when the node is clicked. href="javascript: (js code here)" does pretty much the same thing. Right now it will eval return login(); which is a call to the Javascript function in the global scope named login.
You should find the login() function and read its source to see what it does. You can either modify login() to perform your form submission, or swap out the event handler to call your own function.
You don't want NodeJS (which is javascript for server-side programming), or PhantomJS (which is a web integration testing tool with a Javascript interface). Neither of them apply to this problem domain. If you want a tool to make event handler binding more convenient, try jQuery. It provides DOM manipulation tools and a widely-used Ajax library.
see https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit or http://api.jquery.com/submit/ for details on how to submit a form in your event handler function.
Related
I am using Go's "net/http" package to pass data between the html and the backend in Go. For example, we can used the location of an image like this:
<img src={{.MyPicture}} width=200 height=auto/>
We can do the same thing to pass in functions to the html and call them:
{{if .MyBool}}
{{.MyFunction}}
{{end}}
Now my question is: how do I set the response of a button to call my function? I would expect this to work, but it doesn't:
<button onclick={{.ShowMoreLinks}}>Show more!</button>
I get "[js] Declaration or statement expected." I've tried wrapping it in a script (both inline and in the header), but neither of these seem to work. JS can't handle the passed in variable.
Actually, you're using Go's template mechanism to prepare the HTML that you send to the browser. This only fills in placeholders and ultimately only produces a string (in this case HTML) that will be sent over the network to the browser for interpretation. It's the browser that can only handle interactivity with the user via events such as "onclick". Furthermore, these events have to be Javascript code, like a JS expression or a function call:
onclick="jsFunction()"
You could make your Go template provide the function to be called in the placeholder:
onclick="{{.JSFuncCall}}"
Here ".JSFuncCall" would have to evaluate to some JS function name (and the parentheses to make the call) that you must have defined in your JS client-side code. That Javascript function could then make an XHR call to the server at some specific URL that triggers a Go handler, runs the Go code you want to run, and then returns a response that you can then handle back in the JS function.
I have a caching problem on a button event in Java EE. In JSP, I call an inline JavaScript method in JSP page. This method requests a function in a Java class. Communication between the JSP and the method of java class occurs via structs. The problem is that after I modified this java class, all JSP screen links squeegee when the application calls the same JavaScript method. But this method is only called through a onClink() event is a screen button. Now all JSP buttons are calling this method, even if your event JavaScript onClink is calling another method. If the problem is not the call of JavaScript methods, how do I solve this problem? Refreshing this page does not.
An alternative would be to clear the cache only javascript. And keep the session java application and any other infromação of aplciação java web browser. The problem began to occur when I made an appointment in a java method using as parameter the "ApplicationForm" that takes the form data and use it as research. But for some reason this confunfoiu the browser
I do not want to sound ridiculous, but I can not put the code here.It is copyrighted and owned the company. But it is quite simple. Generic something would be.
JSP Page
The method includes call
// code jsp
<script>
incluirChamada function () {
// Calls the "javascript: incluirChamadaJava" java class via structs
}
the problem is that all the buttons call the incluirChamada () method. even if your onclink are specifying to another class
In the java code ...
javascript: incluirChamadaJava (ApplicationForm form) {
mehod slectUsingHibernate (Filter form) {
}
}
debug your code. You problem have some function that is not closed properly.
I am using JSF on my project.
My scenario is that I have a form with a lot of input fields, and before calling the action on the a4j:commandButton I will be converting the input fields into JSON Data using javascript.
I haven't tested with a really large Form, my current form seems to be submitted correctly upon 'onclick'.
My question is would all of the javascript written on the attribute 'onclick' finish first before running the 'action'?
I've read some articles across the web and it says that
onclick = JavaScript code to be invoked before Ajax request is sent.
However it does not say that it would finish all up the javascript before invoking 'action'
Could you clear this up for me?
Thanks!
Here's a sample code
function transformToJson(object){
//transform all input fields to json data
}
<a4j:commandButton onclick="submitJsonObject(transformToJson('.myForm'))"
value="Save"
action="#{bean.save}">
</a4j:commandButton>
<a4j:jsFunction name="submitJsonObject">
<a:actionparam name="param1" assignTo="#{model.jsonData}" />
</a4j:jsFunction>
The submitJsonObject is a a4:jsFunction, it will send the jsondata into my model, would the transformToJson and submitJsonObject finish before actually invoking the action="#{bean.save}"?
First of all the browser should not submit any data at all before the onclick method returns, since returning false will signal the browser not to submit at all.
In all cases and frameworks i have seen the generated code will either use the browsers own internal submit, that is to say no additional code will be added to the onclick event OR append any required code after the user defined code in the onclick handler.
If in doubt use your browsers inspection tools, but its fairly safe to assume all code will be executed prior to submit.
I have a HTML like the following:
<div>
<form>
<input type="text" />
<button class="sendForm" value="Send form" />
</form>
</div>
<script>
// post the form with Jquery post
// register a callback that handles the response
</script>
I use this type of form a lot with a JavaScript/JQuery overlay that displays the form. That could be handled for example with plugins like FancyBox. I use Fancybox for Ajax content.
I also want to use this form embedded into a GWT view. Lets assume that the for cannot be created on client side because it has some server based markup language inside to set up some model data.
If I want to use this form in GWT I have to do the following. Tell GWT the form request url and use a RequestBuilder to query the html content of this form. Then I can insert it into a div generated by GWT. So far so good.
Problem:
When the user hits the send button the response is handled my the JQuery callback that is inside the script under the form.
Is there a way to access this callback from within GWT?
Is there a way to overwrite the JQuery send action? Since, the code is HTML and comes from the server I cannot place ui-binder UiFields inside to get access to these DOM elements.
I need to get the response if the submitted form accessible to GWT.
Is there a way how I can achieve this with JSNI?
Answers to each question:
1 Is there a way to access this callback from within GWT? actually you cannot modify the callback itself, what you can do from GWT is to call any jquery method, thus you can unbind any previous added handler, and set yours.
//NOTE: not wrapping code in $entry() to make a clearer code.
private static native unbindForm() /*-{
// remove all jQuery handlers added previously to my form
$wnd.$("my_form_selector").off();
// add a new handler for the submit event
$wnd.$("my_form_selector").on("submit", function(event) {
event.preventDefault();
$wnd.$(this).post(url, ...).done(function(data) {
// Use JSNI to call a GWT method
#.com.example.MyClass.handleFormResponse(*)(data);
// NOTE: that you can use a star instead of having to write parameter
// types, when you only have one method signature in your class.
});
}
}-*/
// handle the form response in GWT
public static void handleFormResponse(String data) {
// handle form response in GWT
}
Another thing you can do with GWT, is to wrap your form in a FormPanel and use specific widget methods to set a handler and read the response. But I think this is not what you are asking for.
2 Is there a way to overwrite the JQuery send action Yes, using JSNI to ask jQuery to unbind previously set events. See code in #1.
3 I need to get the response if the submitted form accessible to GWT. You have to include in the jquery.post callback some code to call GWT static methods (you can use-non static as well but this way is easier) this is also JSNI. See code in #1.
4 Is there a way how I can achieve this with JSNI? Of course, JSNI is the way to interact with handwritten javascript from GWT.
Aditional Notes:
GWT is designed to build RIA apps with very optimized js code. I know each one has their reasons for using external libraries, but adding 3party javascript to your app is against the main goals of gwt compiler remove death code and optimize output. If you like jquery like syntax and features for GWT I recomend to use gwtquery, which has been fully written in gwt, hence the compiler will include just the code you use.
Writing JSNI is a source of mistakes difficult to handle in the debugger. So I recommend to use gwt-exporter to populate java methods/classes or gwtquery to call external javascript. See these post I wrote some time ago: Pass value from GWT to Javascript via JSNI and Calling GWT Java function from JavaScript
Javascript => GWT and GWT => javascript values passing both can be done using JSNI . Please have a look here for more information about JSNI
I want to know why we use __doPostBack methods. Is it a build-in JavaScript method or a user defined function and what does this code do?
onclick="javascript:setTimeout('__doPostBack(\'SectionA$1\',\'\')', 0)
I am getting an error for this method:
object expected
doPostBack is a javascript function that asp.net uses to submit the main form for executing the server-side code of your application.
Buttons are postbacks after clicking on them, however some other controls does not postback. So they calls the doPostBack javascript for simulate the postback action.
In your case, you might having trouble with your server-side control. This usually happens while having some problem about configuring the controls that uses the mechanism above. So you are interested with the wrong side now I suppose.
It's __doPostBack with two _ characters.