Phillip Senn
Phillip Senn

Reputation: 47625

jQuery follow hyperlink with a target="_blank"

I am trying to open a hyperlink with target="blank".

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
jQuery(window).load(function() {
    $('a').click();
});
</script>
</head>

<body>
<a href="http://www.google.com" target="_blank">Report</a>
</body>
</html>

Upvotes: 2

Views: 3233

Answers (4)

Phillip Senn
Phillip Senn

Reputation: 47625

I used a normal target="_blank" hyperlink to page2 and had page2 do the database processing.

Upvotes: 0

Ely
Ely

Reputation: 3240

It seems that the trigger events only fire events that have been linked using jQuery. Look at this modified example:

$(document).ready(function() {
    $('a').click(function(){
        alert('adf');
    });
    $('a').click();
});

Upvotes: 3

defrex
defrex

Reputation: 16435

Click, according to the jQuery docs,

"Causes all of the functions that have been bound to that click event to be executed."

This would not include opening the link.

I'm not sure how one would do it otherwise. In fact I'm fairly certain that it cannot be done.

Upvotes: 2

ceejayoz
ceejayoz

Reputation: 180023

click() creates an on-click event and attaches it to the object. It doesn't actually cause a click, it just creates a function that executes after a user's click if/when they choose to do so.

What you're looking for is trigger(), which lets you simulate a click without the user actually initiating it. Also, you should the ready event instead of the load event in most cases.

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
jQuery(window).ready(function() {
    $('a').trigger('click');
});
</script>
</head>

<body>
<a href="http://www.google.com" target="_blank">Report</a>
</body>
</html>

Another thing to note - what you're trying to do may be blocked by popup blockers, as they're designed to stop a website from opening a new window when the page is loaded.

Upvotes: 2

Related Questions