Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the window.open functionality?

Let's say I have window.open (without name parameter), scattered in my project and I want to change the implementation so that wherever name is not specified I'll specify a default name.

What I want to do about this is hook my own method to window.open so that whenever window.open runs it'll internally call my own method which will then call window.open (with the name parameter).

Is that possible through Javascript? Will there be any circular dependency issues in this i.e. window.open calling my custom function which in turn calling the window.open function again?

P.s. In simple terms what I want to do is override the window.open functionality.

like image 795
Sidharth Panwar Avatar asked Feb 07 '12 07:02

Sidharth Panwar


People also ask

How do I get a return value from a Windows Open?

Typically the onclick event on the "Yes" or "Ok" button in the modal dialog looks like this: window. returnValue = true; window. close();


3 Answers

To avoid circular calls, you need to stash away the original window.open function in a variable.

A nice way (that doesn't pollute the global namespace) is to use a closure. Pass the original window.open function to an anonymous function as an argument (called open below). This anonymous function is a factory for your hook function. Your hook function is permanently bound to the original window.open function via the open argument:

window.open = function (open) {
    return function (url, name, features) {
        // set name if missing here
        name = name || "default_window_name";
        return open.call(window, url, name, features);
    };
}(window.open);
like image 60
Ates Goral Avatar answered Sep 23 '22 08:09

Ates Goral


I know this reply is a little late, but I felt that a more general solution may be helpful for other people (trying to override other methods)

function wrap(object, method, wrapper){
    var fn = object[method];

    return object[method] = function(){
        return wrapper.apply(this, [fn.bind(this)].concat(
            Array.prototype.slice.call(arguments)));
    };
};

//You may want to 'unwrap' the method later 
//(setting the method back to the original)
function unwrap(object, method, orginalFn){
    object[method] = orginalFn;
};

//Any globally scoped function is considered a 'method' of the window object 
//(If you are in the browser)
wrap(window, "open", function(orginalFn){
    var originalParams = Array.prototype.slice.call(arguments, 1);
    console.log('open is being overridden');
    //Perform some logic
    //Call the original window.open with the original params
    orginalFn.apply(undefined, originalParams); 
});
like image 33
mpd Avatar answered Sep 24 '22 08:09

mpd


P.s. In simple terms what I want to do is override the window.open functionality.

var orgOpen = window.open;

window.open = function (...args) {
    alert("Overrided!"); 
    return orgOpen(...args); 
}

window.open("http://www.stackoverflow.com");
like image 8
fardjad Avatar answered Sep 21 '22 08:09

fardjad