Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Automating click via Javascript

 
Terry Bailey
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Terry Bailey
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic