I am having anchor tag in my page. I like to trigger click event onload . Which means I wanna open this page “http://XXXXX.com” with new tab. Because I don’t wanna popup blockers. Is there anyway to do this?
anchor attrs are given bellow
id="add_redirect"
href="http://XXXXX.com"
target="_blank"
If your goal is to bypass pop-up blockers on page load, triggering the
clickevent synthetically probably won’t work. Browsers are smart enough to know when aclickis user-generated vs. when you’ve called theclickfunction on the DOM element (on those browsers were that even works). Examples: http://jsbin.com/avibi3/3, http://jsbin.com/avibi3/4Using jQuery’s
triggermechanism certainly won’t do it, because it doesn’t really trigger aclickevent at all; it just fires the handlers that jQuery hooked up (edit: and, apparently, ones defined via anonclickattribute — see Sukhi’s answer — but not ones attached viaaddEventListener). If that’s what you want to do, Sukhi’s answer shows you how, although I always say: If you want code to be run from two different places, put it in a function, and call that function from two different places (rather than putting it in aclickhandler and then simulating a click just to run the code). There are valid use cases fortrigger(mostly relating to integrating with third-party scripts), but for running your own code from two different places, it’s a symptom of a design problem.