Is there a way to programmatically within javascript call the native click event of an tag? The .trigger('click') or .click() functions will not work, because they are triggering the onClick event of the link and not the event that follows the URL. I need to programmatically click a hidden link and follow that link to a new tab within the same browser. Window.open won't work either because that will cause a popup and I am trying to avoid that. I need this, because I am dynamically generating a URL on the server side and sending the responds back via JSON. I then populate the HREF attribute of the hidden link with the resulting URL. If I can get an answer on how this can be done, I would greatly appreciate it.
+1
A:
If you're populating a HREF with the JSON response, why not just store it in a variable instead, and when you want to go to it, window.location = yourVariable;
?
Robert
2010-10-27 19:18:40
window.location wont work for me, because I don't want to open the link in the current tab and replace the current page. I need it to go to a new tab.
usherjer
2010-10-27 19:20:30
Agreed, I don't see the point of assigning the URL to a hidden link just so you can click it. There are far more direct ways provided for navigating to a new location.
meagar
2010-10-27 19:20:48
@usherjer If you want it to open in a new tab, use `window.open`. Whether that opens in a tab or a new window is entirely browser dependent as not all browsers support tabs. There is no way to specifically direct a browser to open a link in a new tab.
meagar
2010-10-27 19:21:33
The reason I am trying to put it in a hidden link is because the functionality of the link click allows for a new tab to open while never replacing the current tab. If I use window.location, it replaces the current tab with the new URL.
usherjer
2010-10-27 19:22:37
@usherjer: There really isn't a guaranteed way to ensure that a link will be opened in a new tab, as the browser looks to it's internal settings for that option. If my browser is configured to open every link in the same window, your only other option is `window.open`.
Robert
2010-10-27 19:22:55
I can use window.open, but I might need to open more than one tab/popup at a time and once I have a popup open, I can't seem to open another unless I close it first.
usherjer
2010-10-27 19:24:00
+1
A:
Unless you want this to work on IE only, I don't have an answer for you, but I can tell you what the problem is. The problem is target=_blank
. Try the following code as a test--notice it will bring you to Google.com--then try writing "_blank" in as the target. That seems to be the only thing that the browsers won't allow. A partial answer to your question is fireEvent()
works with IE and dispatchEvent()
works with everything else. If anyone else could improve on this code, I would appreciate a better answer as well.
<html>
<head>
<script type="text/javascript">
window.onload = function fireElement() {
var target=document.getElementById('foobar');
if(document.dispatchEvent) { // W3C
var oEvent = document.createEvent( "MouseEvents" );
oEvent.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, target);
target.dispatchEvent( oEvent );
}
else if(document.fireEvent) { // IE
target.fireEvent("onclick");
}
}
</script>
</head>
<body>
<div>
<a id="foobar" target="" href="http://www.google.com"></a>
</div>
</body>
</html>
bozdoz
2010-10-28 02:51:56