Sometimes you might need to make a change to the default behavior of a Javascript function and want to apply it to all pages. An example could be to do some custom behavior replacing all new window popups with jQuery modal dialogs across an existing application. Instead of going through the code and finding all instances of window.open() and replacing with modal dialogs you could override window.open() itself and do it in one place.
Here’s a neat technique to override default functions without corrupting the global namespace.
Code:
Example 1: Override but do nothing, call window.open:
Example 2: Override but do nothing, call showModalDialog function: (Leaving out the actual implementation of showModalDialog() for brevity)//override globallywindow.open = function (open) {return function (url, name, features) {// some browsers dont support the call() method, so call the method directly in such casesreturn open.call ? open.call(window, url, name, features):open( url, name, features);};}(window.open);// pass in the window.open function object as argument to the anonymous function
//override globallywindow.open = function (open) {return function (url, name, features) {// some browsers dont support the call() method, so call the method directly in such casesreturn open.call ? showModalDialog.call(window, url, name, features):showModalDialog( url, name, features);};}(window.open);// pass in the window.open function object as argument to the anonymous function
No comments:
Post a Comment