简体   繁体   中英

How to override window.open?

I was wodering, is it possible to override window.open? I want to a add delay to every window.open call

Let's say one piece of code opens window using onClick, the other one using window.open, so is it possible to add like setTimeout to global window.open ?

First save a reference to window.open , then overwrite it with your own function that calls the saved reference after the timeout:

const origOpen = window.open;
window.open = (url) => {
  setTimeout(() => {
    origOpen(url);
  }, 1000);
};


window.open('https://www.google.com');

(cannot embed into live snippet due to sandboxing constraints, but you can see a live demo here )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM