Tags
Asked 2 years ago
9 Jun 2021
Views 219
Harley

Harley posted

How to override the window.open functionality?

How to override the window.open functionality?


window,open("https://www.java.com");


i want to override already created window or before it created with window.open
steave ray

steave ray
answered Apr 27 '23 00:00

To override the window.open() method in JavaScript, you can simply assign a new function to the window.open property.

Here is an example of how to override the window.open() method to log the URL that is being opened:



window.open = function(url) {
  console.log("Opening URL:", url);
  return null; // Return null to prevent opening the URL in a new window
};

This code creates a new function and assigns it to the window.open property. The new function logs the URL that is being opened using console.log() . It then returns null to prevent the URL from being opened in a new window.

You can cal l window.open() as usual, and the new function will be called instead of the original window.open() method:



window.open("https://www.example.com");

This will log the message "Opening URL: https://www.example.com" to the console instead of opening the URL in a new window.
Post Answer