tags:

views:

235

answers:

4

Is it possible to override somehow document.location.href ?

need override getter, ex.: alert(document.location.href); should return lets say "www.example.com" while real document location is www.stackoverflow.com...

don't know is it possible..

A: 

No. It is not possible for security reasons.

alemjerus
What security reasons? I can't think of one right now.
Georg
gs: You could set up a fake Facebook sign-in page and the change the URL onload.
Jan Aagaard
This actually depends on the browser. Some browsers will allow you to replace the location object and fake the return from this API. Websites should not rely upon this object as a means of protecting from attack.
EricLaw -MSFT-
There's no connection between the javascript variables and the URL. Returning wrong data from `document.location` doesn't affect the URL field of the browser.
Georg
+1  A: 

No, but...

In Ecmascript 5, there is support for getters/setters and you can spoof the document reference if accessed from within a scope which redefines it.

Proof:

(function (document) {
    alert(document);        // -> "spoofed document" 
})("spoofed document");

Combined with accessors you can replace the document object. (Javascript 1.5 is needed for accessors.)

Mikuso
Note that it's still easy to break out of this sandbox using various commands which perform an `eval()` internally or directly using `eval()`. For an example see here: http://jsbin.com/esula/edit
Georg
A: 

As others already have noted it is not possible to change the URL without reloading the page.

Note that you can the change the fragment identifier, i.e. the part in the URL after the hash (#) using document.location.hash, but that is probably not good enough for you.

Jan Aagaard
A: 

Swap out "document" to another variable, edit it, and swap it back in.

var d = {}
for (var k in document) {
    d[k] = document[k];
}
d["location"]="out of this world";
document = d;
mparaz