tags:

views:

483

answers:

4

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"&gt;&lt;/script&gt;
<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>
+1  A: 

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"&gt;&lt;/script&gt;
<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.

ceejayoz
Actually click() does fire a click event just like trigger. It only register an event handler if you pass it an argument. Check out the jquery docs: http://docs.jquery.com/Events/click
TM
I read the docs.jquery.com page you referred to, but there was no example of how the cick event was used.
cf_PhillipSenn
Thanks ceejayoz, but this still isn't working.jQuery(function($) { $('a').trigger('click');});
cf_PhillipSenn
Its the first listing, the one without any parameters.
Ely
@psenn Sounds like you're running into a popup blocker or something, then. The only other thing I can suggest right now is to give the link an ID and access it that way.
ceejayoz
+1  A: 

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.

defrex
+1  A: 

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();
});
Ely
Hmm. That's weird. Maybe what I'm wanting is a feature that has been exploited by hackers, so is no longer allowable.As the page loads, I need it to load another page on top of it like target="_blank"
cf_PhillipSenn
A: 

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

cf_PhillipSenn