Simulating Click on hyperlinks in Firefox

Posted on 2009-04-15
Last Modified: 2013-12-07
I am trying to use the following code to simulate a click on a hyperlink...

function doClick(obj) {
        try {
            var evt = document.createEvent("MouseEvents");
            evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,false, false, false, false, 0, null);
            var canceled = !obj.dispatchEvent(evt);
            if (canceled) {
                // A handler called preventDefault
            } else {
            // None of the handlers called preventDefault
           // alert(0);
        } catch (er) {
  ; //IE

This works for all browsers (Chrome, IE, Safari) but not Firefox.
I think the original solution was written for an earlier version of Firefox.
the hyperlink element looks like this...

<a href="new.aspx" target="_blank" title="blank" id="clickme"  >Click Me</a>

Any ideas?
Question by:davidoxley
    LVL 3

    Expert Comment

    Use the code below. It generates a click() function for the objects (which is inbuilt in IE). = function() {
    	var evt = this.ownerDocument.createEvent('MouseEvents');
    	evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);

    Open in new window


    Author Comment

    this works for other elements.  I tried a checkbox and it works great on all browsers.
    It doesnt work for hyperlinks (<a href...)

    Author Comment

    ok, i discovered a little more..
    Firefox doesnt fire the default event action when you simulate a click.
    You need to supply an onclick="" to the anchor element.

    I could say onclick="location.href=this.href;"
    but i want to open a new window without resorting to using, which gets trapped by popup lockers.  thus the orginal anchor had the target="_blank" attribute
    any ideas?

    LVL 3

    Expert Comment

    The simplest way would be using the function
    If you don't want to use that you may create a form with target="_blank" and submit it. Something like the code in the snippet:

    form = document.createElement("form");
    form.method = "GET";
    form.action = ""; = "_blank";

    Open in new window


    Author Comment

    Firefox traps this approach as a popup
    LVL 3

    Accepted Solution

    I'm not sure, if there is another way to open a link in a new tab/window....

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
    How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now