?
Solved

Simulating Click on hyperlinks in Firefox

Posted on 2009-04-15
6
Medium Priority
?
1,501 Views
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) {
                //alert(1);
                // A handler called preventDefault
            } else {
            // None of the handlers called preventDefault
           // alert(0);
            }
        } catch (er) {
            obj.click(); //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?
0
Comment
Question by:davidoxley
  • 3
  • 3
6 Comments
 
LVL 3

Expert Comment

by:Bortian
ID: 24155522
Use the code below. It generates a click() function for the objects (which is inbuilt in IE).

HTMLElement.prototype.click = 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);
	this.dispatchEvent(evt);
}

Open in new window

0
 

Author Comment

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

Author Comment

by:davidoxley
ID: 24156419
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 window.open, which gets trapped by popup lockers.  thus the orginal anchor had the target="_blank" attribute
any ideas?


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Bortian
ID: 24157561
The simplest way would be using the window.open() 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 = "http://www.your.link.url.com";
form.target = "_blank";
document.body.appendChild(form);
form.submit();

Open in new window

0
 

Author Comment

by:davidoxley
ID: 24162028
Firefox traps this approach as a popup
0
 
LVL 3

Accepted Solution

by:
Bortian earned 1500 total points
ID: 24165098
I'm not sure, if there is another way to open a link in a new tab/window....
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month16 days, 10 hours left to enroll

864 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