admin管理员组

文章数量:1392007

Ok, so I searched all over with no answer. Can someone explain why

does not work with .trigger('click')

<a id="openNew" href="">Click me</a>

<script type='text/javascript'> 
    $(window).load(function(){
        $('#openNew').addClass("external").attr({ target: "_blank", href: '' }).trigger('click');
    });
</script>

and it does not work with .click()

<script type='text/javascript'> 
    $(window).load(function(){
        $('#openNew').addClass("external").attr({ target: "_blank", href: '' }).click();
    });
</script>

Does not click the link whatever I do. It only works if I click it. How can I make it auto click? Working on this for about 1 hour and is driving me crazy, I know I'm must be doing something stupid.

JsFiddle for your convenience.

I wouldn't mind any other solution in plain JavaScript.

Ok, so I searched all over with no answer. Can someone explain why

does not work with .trigger('click')

<a id="openNew" href="http://www.example">Click me</a>

<script type='text/javascript'> 
    $(window).load(function(){
        $('#openNew').addClass("external").attr({ target: "_blank", href: 'http://www.google.' }).trigger('click');
    });
</script>

and it does not work with .click()

<script type='text/javascript'> 
    $(window).load(function(){
        $('#openNew').addClass("external").attr({ target: "_blank", href: 'http://www.google.' }).click();
    });
</script>

Does not click the link whatever I do. It only works if I click it. How can I make it auto click? Working on this for about 1 hour and is driving me crazy, I know I'm must be doing something stupid.

JsFiddle for your convenience.

I wouldn't mind any other solution in plain JavaScript.

Share Improve this question edited Oct 15, 2015 at 17:30 Captain Obvlious 20.1k5 gold badges48 silver badges80 bronze badges asked Sep 1, 2011 at 7:11 Mihai IorgaMihai Iorga 39.7k17 gold badges108 silver badges109 bronze badges 2
  • This is a duplicate question. You're asking how to simulate a click of an anchor tag using jquery click/trigger. Here is a previous post with your answer. – kasdega Commented Sep 1, 2011 at 7:32
  • JsFiddle Showing that the click even is being triggered. – kasdega Commented Sep 1, 2011 at 7:35
Add a ment  | 

4 Answers 4

Reset to default 1

Simulating a user physically clicking the link is not possible. Since you are using target='_blank' I presume you want a new window? So you'll need to use window.open. Which popup blockers wont like.

Use elem[0].click(); instead of elem.click(); since you want to call the native click function and not just trigger the click event.

By the way: Popup blockers will prevent this from actually opening a new window (luckily).

Actually it's clicked, but not opened link.. check out here http://jsfiddle/H2KuF/5/

Probably you need to open new browser window with this link from JS.

here is samples I found:

function open2(url, opt){
  if (opt == 0) // current window
    window.location = url;
  else if (opt == 1) // new window
    window.open(url);
  else if (opt == 2) // background window
    {window.open(url); self.focus();}
}

Karl swedberg states here (One of the ments)

Using .trigger('click') will not trigger the native click event.

Doing the following will work:

<a id="openNew" href="http://www.example">Click me</a>

<script type='text/javascript'>
    $(window).load(function(){
        $('#openNew').addClass("external").attr({ target: "_blank", href: 'http://www.google.' })[0].click();
    });
</script>

Demo here

本文标签: javascriptjQuery auto click on a linkStack Overflow