最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Override document.location.href is it possible in javascript? - Stack Overflow

programmeradmin7浏览0评论

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

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

don't know is it possible..

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

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

don't know is it possible..

Share Improve this question asked Jan 11, 2010 at 15:37 rondrond 411 silver badge2 bronze badges 2
  • 3 Perhaps if you tell us what you actually want to achieve, a solution might be forthing. The best way to do what you seem to have asked for is alert("www.example."); – Paul Butcher Commented Jan 11, 2010 at 15:46
  • I believe his website uses plug-ins, which shouldn't have access to the URL. – Georg Schölly Commented Jan 11, 2010 at 16:11
Add a ment  | 

5 Answers 5

Reset to default 2

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.)

No. It is not possible for security reasons.

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.

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;

You can override it in IE7 or IE8 by making use of independent script tags (but not in modern Firefox or IE9+):

<!DOCTYPE html>
<html><head>
<title>Some title</title>
</head>
<body>
<script>
var _oldDoc = document; // IE < 9 won't work with the code below unless we place it here in its own script tag
</script>
<script>
var document = {};
for (var k in _oldDoc) {
    if (navigator.appName == 'Microsoft Internet Explorer' || 
        !k.match(/^(location|domain|body)$/) // Cause problems or errors in Mozilla, but Mozilla isn't really creating a new document object anyways
    ) {
        document[k] = _oldDoc[k];
    }
}

// Causes problems in Mozilla as we can't get Mozilla to actually overwrite the object
document["location"] = {
    href: "out of this world",
    toString: function () {
        return this.href;
    }
};
alert(document.location.href); // "out of this world" in IE < 9
alert(document.location); // "out of this world" in IE < 9
alert(document.title); // 'Some title'
</script>
<script>
alert(document.location.href); // also "out of this world" in IE < 9
alert(document.location); // also gives "out of this world" in IE < 9
alert(document.title); // also 'Some title'
</script>

</body>
</html>
发布评论

评论列表(0)

  1. 暂无评论