dilip
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 call 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.