0

So this is just a small personal project that I'm working on using awesomium in .net. So in awesomium I have this browser open and all that and I want to click this button that has this code.

<a class="buttonright" >&nbsp;Bump&nbsp;</a>

But considering it's a class and not a button I'm having trouble finding a way to "click" it. My plan is to use javascript in awesomium to click it but maybe I'm approaching this from the wrong direction?

Thanks

Timothy Groote
  • 8,614
  • 26
  • 52
Thomja
  • 259
  • 5
  • 15

3 Answers3

2

Update:
After a lot of comments (back and forth) I set up a fiddle, with a working version of this code (the code here works, too, but needed some debugging). The eventTrigger function in the fiddle has been stripped of all comments, but I've added an example usage of this function, which is generously sprinkled with comments.
Browse through it, fork it, play around and get familiar with the code and concepts used there. Have fun:

Here's the fiddle


If by "finding a way to click it" you mean: how to programmatically click this anchor element, then this is what you can use:

Here's a X-browser, slightly verbose yet comprehensive approach:

var eventTrigger = function(node, event)
{
    var e, eClass,
        doc = node.ownerDocument || (node.nodeType === (document.DOCUMENT_NODE || 9) ? node : document);
        //after checking John Resig's Pro JavaScript Techniques
        //the statement above is best written with an explicit 9
        //Given the fact that IE doesn't do document.<NODE_CONSTANT>:
        //doc = node.ownerDocument || (node.nodeType === 9 ? node : document);
    if (node.dispatchEvent)
    {//dispatchEvent method is present, we have an OK browser
        if (event === 'click' || event.indexOf('mouse') >= 0)
            eClass = 'MouseEvents';//clik, mouseup & mousedown are MouseEvents
        else
            eClass = 'HTMLEvents';//change, focus, blur... => HTMLEvents
        //now create an event object of the corresponding class
        e = doc.createEvent(eClass);
        //initialize it, if it's a change event, don't let it bubble
        //change events don't bubble in IE<9, but most browsers do
        //e.initEvent(event, true, true); would be valid, though not standard
        e.initEvent(event, !(event === 'change'), true);
        //optional, non-standard -> a flag for internal use in your code
        e.synthetic = true;//mark event as synthetic
        //dispatch event to given node
        node.dispatchEvent(e, true);
        //return here, to avoid else branch
        return true;
    }
    if (node.fireEvent)
    {//old IE's use fireEvent method, its API is simpler, and less powerful
        //a standard event, IE events do not contain event-specific details
        e = doc.createEventObject();
        //same as before: optional, non-standard (but then IE never was :-P)
        e.synthetic = true;
        //~same as dispatchEvent, but event name preceded by "on"
        node.fireEvent('on' + event, e);
        return true;//end IE
    }
    //last-resort fallback -> trigger any directly bound handler manually
    //alternatively throw Error!
    event = 'on' + event;
    //use bracket notation, to use event's value, and invoke
    return node[event]();//invoke "onclick"
};

In your case, you can use this function by querying the DOM for that particular element, like so:

var elem = document.querySelector('.buttonright');//IE8 and up, will only select 1 element
//document.querySelectorAll('.buttonright'); returns a nodelist (array-like object) with all elements that have this class
eventTrigger(elem, 'click');

That should have the effect of clicking the anchor element

If you're looking for a way to handle click events on this element (an anchor that has a buttonright class), then a simple event listener is all you need:

document.body.addEventListener('click', function(e)
{
    e = e || window.event;
    var target = e.target || e.srcElement;
    if (target.tagName.toLowerCase() === 'a' && target.className.match(/\bbuttonright\b/))
    {//clicked element was a link, with the buttonright class
        alert('You clicked a button/link thingy');
    }
}, false);

That's the cleanest way to do things (one event listener handles all click events). Of course, you can bind the handler to specific elements, too:

var buttons = document.querySelectorAll('.buttonright'),
    handler = function(e)
    {
        alert('Clicked!');
    };
for (var i=0;i<buttons.length;++i)
{
    buttons[i].addEventListener('click',handler, false);
}
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Thank you for your reply but it seems that my question was missunderstood. Using javascript I want to click the button. I don't want an event to trigger after I have clicked it. – Thomja Jun 11 '14 at 09:58
  • @Thomja: triggering a click event is how something is clicked through JavaScript: to JS, a click is an event. The last (most verbose) snippet of code I posted here creates this event, and dispatches it. I've moved the relevant bit of the code to the top meanwhile... That's the same as mimicking a click on the element. Leave out the event listener, and use the last snippet to automatically click the link – Elias Van Ootegem Jun 11 '14 at 10:25
  • Thanks a bunch for the help really apreciate it! But I eventClass is not defined according to the console. Aren't you defining it with "e = doc.createEvent(eventClass);"? – Thomja Jun 11 '14 at 10:40
  • @Thomja: Sorry, my mistake, that should've been `e = doc.createEvent(eClass);`, I've updated my answer. PS: if `DOCUMENT_NODE` is undefined in some browsers, replace it with `9`. On chromium, `DOCUMENT_NODE` is a constant, but I don't know if IE implements these node constants, too – Elias Van Ootegem Jun 11 '14 at 11:20
  • eventName is not defined though. But I really do like the documentation provided with the code! Really helps someone who is not all that into Javascript. – Thomja Jun 11 '14 at 12:00
  • @Thomja: I've decided to actually go and debug the code I posted up here. The `initEvent` call, to which I passed `eventName` should've been `e.initEvent(event, !(event === 'change'), true);` and the `dispatchEvent` call should've passed the actual event object, not the name: `node.dispatchEvent(e, true);` instead of `node.dispatchEvent(event, true);` I've set up a fiddle with a working example, and will ad a link to my answer – Elias Van Ootegem Jun 11 '14 at 12:12
  • Alright thanks for the help I really don't know what to say xD Appreciate it alot. – Thomja Jun 11 '14 at 12:13
  • @Thomja: No need to say anything. In fact, the help section of this site asks you not to post _"thank you"_ comments ([here](http://stackoverflow.com/help/someone-answers)). I'm happy to help, but I would appreciate your upvoting and/or accepting this answer – Elias Van Ootegem Jun 11 '14 at 12:28
  • Worked like a charm! Executed the code in the two boxes at the top. I like that you left me with browser specific options even tho I will only be using awesomium! – Thomja Jun 11 '14 at 19:23
0

Depending on how you want to handle the event, there are numerous roads you can take.

The simplest one is this :

<script type="text/javascript">
  function buttonRight_onclick(event, sender)
  {
     alert("HEY YOU CLICKED ME!");
  }
</script>


<a class="buttonright" click="buttonRight_onclick(event, this)">

whereas if you were using a framework like jQuery, you could do it like this:

<script type="text/javascript">
  $(document).ready(function() {
    $(".buttonright").on("click", function(event) {
      alert("HEY YOU CLICKED ME!");
    });
  });
</script>

<a class="buttonright" >Bump</a>
<a class="buttonright" >Also bump</a>
Timothy Groote
  • 8,614
  • 26
  • 52
  • 1
    The latter approach doesn't require jQ at all. that's merely syntactic sugar for the second approach I showed in vanillaJS... if you _are_ going to use jQ here, `$(document).on('click', '.buttonright', function(){});` would be preferable (delegation!) – Elias Van Ootegem Jun 11 '14 at 09:26
  • And same again, I want to click the button. – Thomja Jun 11 '14 at 09:59
0
<script type="text/javascript">
  function Button_onclick(event, sender)
  {
     alert("Button Clicked!");
  }
</script>

    <a class="Button" click="Button_onclick(event, this)">
Ramniwas chhimpa
  • 205
  • 1
  • 2
  • 10
  • Same here, I want to click the button. Not to have an event firing after I have clicked said button. – Thomja Jun 11 '14 at 09:59
  • As an aside: JS naming conventions dictate function names start with a lower case char (unless they are constructors), and are camelCased. In this case `function buttonClicked` would be the correct function name – Elias Van Ootegem Jun 11 '14 at 11:22