0

Why does a js-generated click on a link with target="_blank" open a new, chromeless window when a user click on the same link opens in a new tab (the desired behavior, and expected with my current browser settings)?

Context: I use the Feedly web app to read RSS feeds, and just tried writing a super-simple bookmarklet to open all the links in a current category in new tabs:

(function () {

  function main() {
    var links = document.querySelectorAll('#timeline .condensedTools a:first-child');
    for(var i = 0; i < links.length; i++){
      var link = document.createElement('a');
      link.href = links[i].href;
      link.target = '_blank';
      document.querySelector('body').appendChild(link);
      link.click();
    }
  }
  main();

})();

I only really care about Chrome for this at the moment, so haven't looked anywhere else. I suppose the browser's approach to popup-ad blocking is forcing a different behavior on these links even though I've expressly allowed popups on this particular site. But is there any way around it for a user-executed script?

Oswald
  • 31,254
  • 3
  • 43
  • 68
RwwL
  • 3,298
  • 1
  • 23
  • 24
  • 1
    possible duplicate of [JavaScript open in a new window, not tab](http://stackoverflow.com/questions/726761/javascript-open-in-a-new-window-not-tab) – Matteo Tassinari Jan 16 '14 at 14:53
  • 4
    @MatteoTassinari This is hardly a duplicate. Please read more than the title (or in this case, read the full title) when marking duplicates. – Tibos Jan 16 '14 at 14:55
  • Yeah, thank you Tibos, this is is definitely not a duplicate of that. I searched first. – RwwL Jan 16 '14 at 14:57
  • 1
    I haven't created bookmarklets before, but I'm just wondering... if you already have an array of `` elements, why would you create duplicates of them to js-click on them? Why can't you simply pull a `window.open(links[i].href, '_blank')` in your loop? – Jason M. Batchelor Jan 16 '14 at 14:59
  • @mori I'll try that, I thought I'd better duplicate because I thought I might want to avoid triggering any click handlers Feedly already had on those links. – RwwL Jan 16 '14 at 15:03
  • @mori57 Using window.open (with or without the _target argument passed in) yields the same result as duplicating the links using .click() — I get a new window, unfortunately. – RwwL Jan 16 '14 at 15:08
  • Check differences between event objects that clicked by user and that triggered programmatically. – Givi Jan 16 '14 at 15:55

0 Answers0