简体   繁体   中英

Calling a JavaScript function on click with jsoup

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM