/    Sign up×
Community /Pin to ProfileBookmark

Clicking link from javascript

Hello,

I want to call the click event of the link (anchor tag) on the click of the button. I used this code below in the click event of button to call links click event and it works fine in IE.

document.getElementById(‘linktag’).click();

But, this doesn’t work in Firefox. I googled a bit and found that in firefox, you have to do something more to achieve this behaviour. So, I ended up doing this on button click to work in firefox:

var link=document.getElementById(‘linktag’);
var e = document.createEvent(‘MouseEvents’);
e.initEvent(
‘click’ // event type
,true // can bubble?
,true // cancelable?
);
link.dispatchEvent(e);

The above code does the click on link when I click on the button. But my problem now is that I have defined a link as <a href=”mailto:[email protected]?subject=abc&body=email body”>email </a>

and when click is called and mailto links opens my email client, it somehow ignores the subject and body parameters of the link. It works properly when i actually click a link element. but it doesn’t work when i simulate the click event by code written above. above dispatching event code somehow ignores the link parameters, any idea why?

thanks,

Farooq

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeJan 24.2011 — I'm going to guess that if the link were to a URL with parameters instead of a mailto:, it would probably go to the URL minus the querystring/NVP/URL parameters. I'm guessing that it ignores everything after the question mark.

Unfortunately, I cannot think of a workaround at this time.

^_^
Copy linkTweet thisAlerts:
@farooqpervaizauthorJan 24.2011 — But If I click the link, everything works fine, its just fails when i simulate the click event through javascript. I guess its something to do with dispatch events because in IE it works fine even with link.click() method call.
Copy linkTweet thisAlerts:
@WolfShadeJan 24.2011 — link.click() is IE only. I'm guessing that FF/Opera/Chrome function works but only up to the end of the mailto/URL, ignoring the URL parameters (or in this case, the mailto: parameters of subject and body.)

^_^
×

Success!

Help @farooqpervaiz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...