JS 中document.URL 和 window.location.href 的区别

实际上,document 和 window 这两个对象的区别已经包含了这个问题的答案。

document 表示的是一个文档对象,window 表示一个窗口对象。

一个窗口下面可以有很多的document对象。每个document 都有 一个URL。

但是,这不是所有的区别。当你ctrl + F5 一个链接 http://yourhost.com/#fragment

打印 alert(document.URL ); 和 alert(window.location.href);

发现,这两个的值不一样,

document.URL 的值是 “http://yourhost.com/”

window.location.href 的值是 “http://yourhost.com/#fragment”

差一个 #fragment

所以,如果要用 fragment 进行相应的处理的话,最好是用 window.location.href

否则会出现很奇怪的错误。

另:以下是屏蔽页面使用查看源码和复制拷贝页面内容的js代码

document.oncontextmenu=function(e){return false;}
document.onmousedown = function() {
    return false;
};
document.onselectstart = function() {
    return false;
};

原文地址:https://www.cnblogs.com/wayne173/p/3745997.html