0

How can I open new browser tabs without focus on them?

(Like clicking on the middle button on a mouse.)

Code:

$("html").on  "click", ->
    window.open('http://google.com')
    $(document).focus()

And I want to still be on the current site.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Elad Kaplan
  • 33
  • 1
  • 7

1 Answers1

1

This answer details what seems to be a legitimate way to open a tab in the background. Converting the answer to CoffeeScript, I get:

openNewBackgroundTab = ->
  a = document.createElement("a")
  a.href = "http://www.google.com/"
  evt = document.createEvent("MouseEvents")

  #the tenth parameter of initMouseEvent sets ctrl key
  evt.initMouseEvent "click", true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null
  a.dispatchEvent evt
  return
Community
  • 1
  • 1
woz
  • 10,888
  • 3
  • 34
  • 64