Question: I want to write a bookmarklet that will modify any webpage so that when the JavaScript code on that page does something like `document.location = 'http://site.tld'` or `document.location.href = 'http://site.tld'` the page will open a new tab or window instead of changing location; same as if code was `window.open('http://site.tld')`;

Can I modify the prototype somehow to achieve this goal? (I use this word carelessly because although I've read about modifying prototypes a few times, I've never actually done it.) Or is there some other way?

Answer: 
You didn't say which browser you're using, but the *only* way I can think of to accomplish this is using Firefox's [`Object.watch`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/watch).

```
document.watch('location', function(prop, oldval, newval) {
    window.open(newval);
    return '#';
});

```

This will intervene on any attempt to set `document.location`, open a window with the same argument instead, and then change the assignment to `'#'` instead (which will do nothing, usually).

In bookmarklet form, and expanded to cover both `document.location` and `window.location`:

```
javascript:(function(){var handle=function(prop, oldval, newval){window.open(newval); return '#';};document.watch('location',handle);window.watch('location',handle);})();

```

But even this won't work against assignment to `document.location.href`; the location object is some special thing that `Object.watch` refuses to work on.

If that's not enough, you could use `onbeforeunload` to prompt you when you try to navigate away and cancel it (if that's your goal here).

Best I've got, sorry!
FWIW: This works on the latest firefox to search for the selected text in a new tab:

javascript:Qr=document.getSelection();if(!Qr){void(Qr=prompt('Keywords...',''))};if(Qr)window.open("<http://google.com/search?query=>"+escape(Qr));void(0)