0

I have spent 2 days googling to find a cross-browser solution to simulate a mouse click on an html <a>, but have yet to find a one.

//-- REGARDING javascript's fireEvent (for IE browsers)
var lvs_event = 'click' ;              
var lvo_event = document.createEventObject();             
argo_target.fireEvent( 'on' + lvs_event , lvo_event );
//-------- does not work on either my winXP IE6 or my winVista IE8


//-- REGARDING javascript's dispatchEvent (for non-IE browsers)
var lvo_event = argo_target.ownerDocument.createEvent('MouseEvents') ;
lvo_event.initMouseEvent( 'click' , ... ) ;
argo_target.dispatchEvent( lvo_event ) ;
//-------- does not work on winVista FF3.6

//-- REGARDING inserting location.href
<a href    = '...'
   target  = '...'
   onclick = '...;location.href = this.href;...'
>
<script>
my_a.onclick();
<\/script>
//-------- works consistently BUT literally calls the onclick handler, ignoring all other <_a_> properties such as href and target

//-- REGARDING various jQuery solutions
$('#my_a').trigger('click');
//OR
$('#my_a').click();
//-------- does not work on any browsers (jQuery IS successfully being used for other features however)

MY GOAL: for a flash button mousedown to relay message to js, which in turns automates a <a> click process.

I can of course let flash call js, get the necessary html info from js and return it to flash, which can then do a as3 geturl, but I would prefer to tie into my existing html environment process.

I am currently testing using various versions of ff, ie, opera, safari(for win), chrome on winXP and winVista.

9
  • You can check out this solution: stackoverflow.com/questions/9230308/… it should work in IE just fine. If not, what version of jquery do you use? Commented Feb 12, 2012 at 22:14
  • What do you mean by simulate click? Commented Feb 12, 2012 at 22:16
  • @elclanrs literally the process that begins when a user clicks a particular <_a_> ... if that particular <_a_> has various behaviors bound to its onclick handler, then those should be called. Likewise if that particular <_a_> has a target='_blank' then that should be observed. BUT not merely calling its onclick-assigned handler Commented Feb 12, 2012 at 22:23
  • Oh, then $('#my_a').trigger('click'); should work, don't know why it isn't working... Commented Feb 12, 2012 at 22:33
  • If solution from the link doesn't work, I would try with a newer version of jquery. Commented Feb 12, 2012 at 22:36

1 Answer 1

1

If you want to follow the url on trigger then you have to say so in the function:

html:

<a id="link" href="http://google.com">Link</a>
<a id="trigger" href="#">Trigger link</a>  

jQ:

$('#link').click(function(){ alert('hey'); });
$('#trigger').click(function(){
    var $link = $('#link');
    $link.trigger('click');
    // window.location.href = $link.attr('href');
    window.open($link.attr('href'), '_blank'); // Popup blockers might block this
});
Sign up to request clarification or add additional context in comments.

8 Comments

elclanrs, thanks for responding. This however, does not truly simulate the process, but rather queries various features of the <_a_> and thus simulates those individual features. Which could work, but in my case it ignores the <_a_>'s target='_blank' requirement.
it should also be noted that this solution can be done, according to my testing, browser-safe using pure javascript: my_a_tag.onclick() along with the location.href=my_a_tag.href
lazy? I explained why your solution is not a solution after I tested it and determined what it does ... what part of that is lazy? Plus your solution ignores the target attribute ... see my original post.
I updated it with the target attribute! what I mean is that is so simple to do a google search for that.
elclanrs, you're wrong again ... window.open is the fundamental trigger for pop-up blockers ... web developers have been avoiding it since 2002.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.