aspose file tools*
The moose likes JSF and the fly likes Automating click via Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Automating click via Javascript" Watch "Automating click via Javascript" New topic
Author

Automating click via Javascript

Terry Bailey
Greenhorn

Joined: Nov 17, 2010
Posts: 14
Hi there,

My first post on the forums and I think I'm getting my concepts a bit mixed up.

In my JSF page I have the following link:

<h: outputLink onclick="window.open('launchProject.jsp?proj=12345', 'popupWindowName'); return false;" value="#" id="launchLink">
<h: outputText value="Test launcher" id="launcher"/>
</h: outputLink>

How would I go about automatically firing this link from Javascript. The code snippit is inside a form and view tag but I cant get the id for the <a> element to be able to sucessfully call the click() method on it.

Thanks for you help.
Terry

Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
Whichever framework you are using should have a guessable way of generating the ID. It's usually something like "form:layout-panels:control-id" (if you are unsure, you can just do "View Source" in the browser and work out what convention your framework uses.
Once you know that, it's pretty simple JS to get the element by ID and then invoke the click function.
Even if you can't get the ID, you can either iterate the DOM or get elements by type and iterate those. What exact JS you use depends on what browser you are using.


SCJP6
Terry Bailey
Greenhorn

Joined: Nov 17, 2010
Posts: 14
Hi Jason,

I tried that and found that the format used is "myform:launchLink"

<a id="myform:launchLink" name="myform:launchLink" href="#" onclick="window.open('launchProject.jsp?proj=12345', 'popupWindowName'); return false;">
<span id="myform:launchCO">Test Launcher</span></a>

Which is pretty much what I expected. However through javascript I'm not able to get a handle on this element, which is the root of my problem

var form = document.forms[0];
var fichValue = form['myform:filevalue'].value; <- This works great for another element
var launcher = form['myform:launchLink'].value; <- returns null
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
I am not sure that JS is 100% valid Terry, I would have done it like this:

I don't see why you want the "value" of the anchor, you're going to call its "click" event.
Terry Bailey
Greenhorn

Joined: Nov 17, 2010
Posts: 14
Jason,

You're quite right! The value was a hangover from one of the 100s of tests i was running to get this to work. Thanks a lot, sometimes the simple solution is by far the best

Terry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Automating click via Javascript