javascript中window与document(转载整理)

window对象和document对象的区别

一般来讲,一个window里就是一个document,但是,iframe里面也可以装个document,在iframe里面就有区别了

alert(document.location?===?window.location);?//?true

不要混淆Window对象的location属性和Document对象的location属性。

前者引用一个Location对象,而后者只是一个只
读字符串,并不具有Location对象的任何特性。

document.location与document.URL是同义的,后者在
JavaScript1.1中是该属性的首选名称(因为这样避免了潜在的混淆)。

在大多数情况下,document.location和
location.href是相同的。但是,当存在服务器重定向时,

document.location包含的是已经装载的URL,而
location.href包含的则是原始请求的文档的URL。
document.location和window.location有什么区别就是
document你可以理解为文档,就是你的网页
window理解为窗口,就是你的ie浏览器包含的
无框架:简单的说,没有框架的情况下,是等同的
有框架:在有框架的情况下,最外层是相同的,

在iframe里面的document.location和window.location不同的。
iframe里面的document.location?你看不ie地址变化,只改变iframe部分,
此时的window.location和top.location效果一致
document.location=”url”;(只读)

document.location.reload(“url”;);
window.location=”url”;
location=”url”;
document.href=”url”;
document.location.href=”url”;
document.location.replace=”url”;
document.action=”url”;

document.submit();
document.location.href;和document.location.replace;

都可以实现从A页面切换到B页面,但他们的区别是:
用document.location.href切换后,可以退回到原页面。

而用document.location.replace切换后,不可以通过“后退”退回到原页面。
关于document.location.href或其他可回退的切换方式
document.location?相当于?document.URL?声明了装载文档的URL,
除非发生了服务器重定向,?否则该属性的值与Window.location.href的值是一样的.
history.go(-1);//返回上一页
document.IFRAME名称.location.href=’url’;//改变框架内容

 

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据