I want to change all links assigned to window.location
. So I assume location
has getter & setter . That's why i cloned it & then i override the real window.location
:
var clonedLocation=Object.assign({},window.location);
Object.defineProperty( window, 'location', {
get: function (f) {
return clonedLocation;
},
set:function(o){
if(typeof o==='string'){
o=o+'?id=1';
}
clonedLocation=o;
}
} );
};
The expected behavior (if overriding done ) is , when you write :
window.location='/go';
The script should redirect you to /go?id=1
NOT /go
.
However the actual behavior is that this script redirects to /go
==> Thus, the window.location
setter wasn't overridden ,
How to override the setter of window.location
?