3

is it possible to capture new browser window open event with javascriptand call some functions before? Thanks

Mikerobenics
  • 215
  • 6
  • 14

1 Answers1

9

window doesn't have a beforeload event.

Something like this could work, but keep in mind that overriding window.open is pretty dangerous.

var windowOpen = window.open;

window.open = function(url, name, features, replace) {
    alert("opening a window");
    // do other stuff here
    windowOpen(url, name, features, replace);  
}

window.open("http://www.google.com");
John Conde
  • 217,595
  • 99
  • 455
  • 496
ktusznio
  • 3,585
  • 3
  • 22
  • 21
  • could you elaborate the bad consequences of overriding window.open()? – uowzd01 Sep 22 '14 at 06:39
  • 1
    It's generally a bad idea to override window (or most global) functions because your overridden implementation is likely more error-prone and could produce unintended side-effects for other code relying on window.open. A better strategy is usually to provide a new function that calls window.open AND does whatever else you want window.open to do, and use that in your own code. This way, only code calling your method is affected. – ktusznio Sep 22 '14 at 22:21
  • it just saved my day!! i don't know why it's not marked as correct answer but tq buddy! – p u Jan 05 '19 at 07:39