2

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> 

4 Answers 4

3

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(); }); 
Sign up to request clarification or add additional context in comments.

1 Comment

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"
2

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.

5 Comments

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: docs.jquery.com/Events/click
I read the docs.jquery.com page you referred to, but there was no example of how the cick event was used.
Thanks ceejayoz, but this still isn't working. jQuery(function($) { $('a').trigger('click'); });
Its the first listing, the one without any parameters.
@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.
2

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.