admin管理员组文章数量:1026989
var a = document.createElement('a');
a.setAttribute('target','_blank');
a.click();
The above code doesn't work for me. Does setAttribute
not work if the click event happens in JavaScript
?
var a = document.createElement('a');
a.setAttribute('target','_blank');
a.click();
The above code doesn't work for me. Does setAttribute
not work if the click event happens in JavaScript
?
- I think it requires an href attribute. – Yogi Commented Feb 22, 2016 at 21:48
- This doesn't seem plete enough to do anything at all. – durbnpoisn Commented Feb 22, 2016 at 21:50
2 Answers
Reset to default 4Anchor elements without href attributes aren't linking to anywhere, so they aren't links, so clicking on one doesn't do anything.
When I added setAttribute("href", "something")
it triggered a new window in my test.
IF you also set an href attribute
a.setAttribute('href','https://google.')
Then when you call a.click() what you will most likely see (in Chrome and Canary) is that the browser will block this popup. This is due to the fact that this is pretty much textbook annoying popup, and since the click was not initiated at first by a click from the user, it is seen as spammy.
If you need to do this, it will usually work if you are triggering the inner "click" from a user initiated (onclick) event handler of some type.
var a = document.createElement('a');
a.setAttribute('target','_blank');
a.click();
The above code doesn't work for me. Does setAttribute
not work if the click event happens in JavaScript
?
var a = document.createElement('a');
a.setAttribute('target','_blank');
a.click();
The above code doesn't work for me. Does setAttribute
not work if the click event happens in JavaScript
?
- I think it requires an href attribute. – Yogi Commented Feb 22, 2016 at 21:48
- This doesn't seem plete enough to do anything at all. – durbnpoisn Commented Feb 22, 2016 at 21:50
2 Answers
Reset to default 4Anchor elements without href attributes aren't linking to anywhere, so they aren't links, so clicking on one doesn't do anything.
When I added setAttribute("href", "something")
it triggered a new window in my test.
IF you also set an href attribute
a.setAttribute('href','https://google.')
Then when you call a.click() what you will most likely see (in Chrome and Canary) is that the browser will block this popup. This is due to the fact that this is pretty much textbook annoying popup, and since the click was not initiated at first by a click from the user, it is seen as spammy.
If you need to do this, it will usually work if you are triggering the inner "click" from a user initiated (onclick) event handler of some type.
本文标签: javascriptsetAttribute target blank doesn39t open in new tabStack Overflow
版权声明:本文标题:javascript - setAttribute target _blank doesn't open in new tab - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745658382a2161734.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论