window.location 和 document.location 有什么区别?

发布于 2024-12-11 02:44:05 字数 63 浏览 0 评论 0 原文

window.location 和 document.location 有什么区别?它们都应该引用同一个对象吗?

What's the difference between window.location and document.location? Should both of them reference the same object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

幽梦紫曦~ 2024-12-18 02:44:05

根据 W3C 的说法,它们是相同的。实际上,为了跨浏览器安全,您应该使用 window.location 而不是 document.location

请参阅:http://www.w3.org/TR/html/browsers .html#dom-位置

According to the W3C, they are the same. In reality, for cross browser safety, you should use window.location rather than document.location.

See: http://www.w3.org/TR/html/browsers.html#dom-location

在巴黎塔顶看东京樱花 2024-12-18 02:44:05

获取当前位置对象的规范方法是 window.location (请参阅 1996 年的此 MSDN 页面W3C 草案从 2006 年开始)。

将此与 document.location 进行比较,后者最初仅以字符串形式返回当前 URL(请参阅 MSDN 上的此页面)。可能是为了避免混淆,document.location 被替换为 document.URL(请参阅 MSDN 上),它也是 DOM 级别 1

据我所知,所有现代浏览器都会将 document.location 映射到 window.location,但我仍然更喜欢 window.location,因为这就是我想要的自从我编写第一个 DHTML 以来就一直在使用。

The canonical way to get the current location object is window.location (see this MSDN page from 1996 and the W3C draft from 2006).

Compare this to document.location, which originally only returned the current URL as a string (see this page on MSDN). Probably to avoid confusion, document.location was replaced with document.URL (see here on MSDN), which is also part of DOM Level 1.

As far as I know, all modern browsers map document.location to window.location, but I still prefer window.location as that's what I've used since I wrote my first DHTML.

花开半夏魅人心 2024-12-18 02:44:05

window.location 在所有兼容的浏览器上均可读取/写入。

document.location 在 Internet Explorer 中是只读的(至少),但在 Gecko 中是读/写的-基于浏览器(Firefox、SeaMonkey)。

window.location is read/write on all compliant browsers.

document.location is read-only in Internet Explorer (at least), but read/write in Gecko-based browsers (Firefox, SeaMonkey).

烈酒灼喉 2024-12-18 02:44:05

document.location 最初是只读属性,尽管 Gecko 浏览器 也允许您对其进行分配。为了跨浏览器安全,请改用 window.location

了解更多:

document.location

window.location

document.location was originally a read-only property, although Gecko browsers allow you to assign to it as well. For cross-browser safety, use window.location instead.

Read more:

document.location

window.location

来世叙缘 2024-12-18 02:44:05

有趣的是,如果您有一个名为“location”的框架、图像或表​​单,则“document.location”分别提供对框架窗口、图像或表​​单的引用,而不是 Location 对象。显然,这是因为 document.forms、document.images 和 window.frames 集合名称查找优先于映射到 window.location。

<img name='location' src='location.png'>

if (document.location.tagName == 'IMG') alert('Hello!')

Interestingly, if you have a frame, image, or form named 'location', then 'document.location' provides a reference to the frame window, image, or form, respectively, instead of the Location object. Apparently, this is because the document.forms, document.images, and window.frames collection name lookup gets priority over the mapping to window.location.

<img name='location' src='location.png'>

if (document.location.tagName == 'IMG') alert('Hello!')
堇色安年 2024-12-18 02:44:05

据我所知,两者是相同的。为了跨浏览器安全,您可以使用 window.location 而不是 document.location

所有现代浏览器都将 document.location 映射到 window.location,但我仍然更喜欢 window.location,因为这是我写这篇文章以来一直使用的我的第一个网页。它更加一致。

您还可以看到 document.location === window.location 返回 true,这表明两者是相同的。

As far as I know, Both are same. For cross browser safety you can use window.location rather than document.location.

All modern browsers map document.location to window.location, but I still prefer window.location as that's what I've used since I wrote my first web page. it is more consistent.

you can also see document.location === window.location returns true, which clarifies that both are same.

-残月青衣踏尘吟 2024-12-18 02:44:05

document.location === window.location 返回 true

并且

document.location.constructor === window.location.constructor true

注意:刚刚在 、Firefox 3.6、Opera 10 和 IE6 上测试

document.location === window.location returns true

also

document.location.constructor === window.location.constructor is true

Note: Just tested on , Firefox 3.6, Opera 10 and IE6

薄暮涼年 2024-12-18 02:44:05

是的,它们是一样的。这是浏览器 JS API 中众多历史怪癖之一。尝试做:

window.location === document.location

Yes, they are the same. It's one of the many historical quirks in the browser JS API. Try doing:

window.location === document.location
汐鸠 2024-12-18 02:44:05

考虑到较旧的浏览器,window.location 是两者中更可靠一致的。

window.location is the more reliably consistent of the two, considering older browsers.

执笔绘流年 2024-12-18 02:44:05

现在很少能看到差异,因为 html 5 不再支持框架集。但在我们有框架集的时候,document.location只会重定向正在执行代码的框架,而window.location会重定向整个页面。

It's rare to see the difference nowadays because html 5 don't support framesets anymore. But back at the time we have frameset, document.location would redirect only the frame in which code is being executed, and window.location would redirect the entire page.

红焚 2024-12-18 02:44:05

document.location.constructor === window.location.constructortrue

这是因为它与您在 document.location===window.location 中看到的对象完全相同。

因此无需比较构造函数或任何其他属性。

document.location.constructor === window.location.constructor is true.

It's because it's exactly the same object as you can see from document.location===window.location.

So there's no need to compare the constructor or any other property.

心病无药医 2024-12-18 02:44:05

至少在 IE 中,它对本地文件有一点不同:

document.URL 将返回
"file://C:\projects\abc\a.html"

但 window.location.href 将返回
"file:///C:/projects/abc/a.html"

一种是反斜杠,一种是正斜杠。

At least in IE, it has a little difference on local file:

document.URL will return
"file://C:\projects\abc\a.html"

but window.location.href will return
"file:///C:/projects/abc/a.html"

One is back slash, one is forward slash.

软的没边 2024-12-18 02:44:05

是的,它们是相同的,但是......!

window.location 无法在某些 Internet Explorer 浏览器上运行。

Well yea, they are the same, but....!

window.location is not working on some Internet Explorer browsers.

夜灵血窟げ 2024-12-18 02:44:05

tl;dr 它们实际上总是相同的。首选window.location

Document 对象的位置 getter 步骤是返回相关全局对象的 Location 对象(如果它完全活动),否则返回 null。

Window 对象的位置 getter 步骤是返回该对象的 Location 对象。

https://html.spec.whatwg .org/multipage/nav-history-apis.html#dom-location

它们什么时候不同?如果文档未完全激活。例如:

const iframe = document.createElement("iframe");
document.body.append(iframe);
const { contentDocument, contentWindow } = iframe;
iframe.remove();
contentWindow.location; // not null
contentDocument.location // null

tl;dr They are virtually always the same. Prefer window.location.

The Document object's location getter steps are to return this's relevant global object's Location object, if this is fully active, and null otherwise.

The Window object's location getter steps are to return this's Location object.

https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location

When are they different? If the document is not fully active. For example:

const iframe = document.createElement("iframe");
document.body.append(iframe);
const { contentDocument, contentWindow } = iframe;
iframe.remove();
contentWindow.location; // not null
contentDocument.location // null
我一向站在原地 2024-12-18 02:44:05

我想说 window.location 是获取当前 URL 的更可靠方法一个>。
以下是我在 URL 中附加哈希参数并稍后读取它的场景之一中前面的 window.locationdocument.url 之间的区别。

在 URL 中添加哈希参数后。

在较旧的浏览器中,我无法使用 document.url 从 URL 获取哈希参数,但是当我使用 window.location 时,我能够获取来自 URL 的哈希参数。

因此,最好使用 window.location

I would say window.location is the more reliable way of getting the current URL.
Following is the difference between the window.location and document.url that came in front in one of the scenarios where I was appending hash parameters in the URL and reading it later.

After adding hash parameters in the URL.

In an older browser, I was not able to get the hash parameters from the URL by using document.url, but when I used window.location then I was able to get the hash parameters from the URL.

So it's always better to use window.location.

不再见 2024-12-18 02:44:05

我更喜欢使用 document.location,即使 locationdocument.locationwindow.location 返回相同的对象。

使用 document.location 的原因是:

  1. window.location 提及

在 Firefox 57 之前,通过 URL API 访问时,URL 中包含的单引号会被转义。请参阅错误 1386683。

  1. document.location< 的浏览器兼容性部分/a> 提及

全力支持。

  1. Mdn 位置参考使用 document.location 在他们的示例中。

    // 位置:https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container
    const loc = 文档.location;
    console.log(loc.href); // https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container
    console.log(loc.协议); // https:
    console.log(loc.host); //developer.mozilla.org:8080
    console.log(loc.主机名); // 开发者.mozilla.org
    console.log(loc.port); // 8080
    console.log(loc.路径名); // /en-US/搜索
    console.log(loc.search); // ?q=URL
    console.log(loc.hash); // #search-结果-关闭-容器
    console.log(loc.origin); // https://developer.mozilla.org:8080
    
    location.assign('http://another.site') // 加载另一个页面
    

I prefer using document.location, even though location, document.location, and window.location returns same object.

Reasons for using document.location are:

  1. Browser compatibility section of window.location mentions

Before Firefox 57, single quotes contained in URLs were escaped when accessed via URL APIs. See bug 1386683.

  1. Browser compatibility section of document.location mentions

Full support.

  1. Mdn location reference uses document.location in their examples.

    // location: https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container
    const loc = document.location;
    console.log(loc.href);      // https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container
    console.log(loc.protocol);  // https:
    console.log(loc.host);      // developer.mozilla.org:8080
    console.log(loc.hostname);  // developer.mozilla.org
    console.log(loc.port);      // 8080
    console.log(loc.pathname);  // /en-US/search
    console.log(loc.search);    // ?q=URL
    console.log(loc.hash);      // #search-results-close-container
    console.log(loc.origin);    // https://developer.mozilla.org:8080
    
    location.assign('http://another.site') // load another page
    
葬シ愛 2024-12-18 02:44:05

尽管大多数人都在这里推荐,但这就是 Google Analytics 的动态协议剪裁的样子(在他们最近从 ga.js 迁移到 Analytics.js 之前):

ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

更多信息:< /em> https://developers.google.com/analytics/devguides/collection/gajs/

在新版本中,他们使用“//”,以便浏览器可以自动添加协议:

'//www.google-analytics.com/analytics.js'

所以如果 Google 当他们需要 JS 中的协议时,他们更喜欢 document.location 而不是 window.location,我猜他们有一些原因。

总体:我个人认为 document.locationwindow.location 是相同的,但如果拥有关于浏览器使用情况的最大统计数据,例如 < strong>Google 使用 document.location,我建议关注它们。

Despite of most people recommend here, that is how Google Analytics's dynamic protocol snipped looked like for ages (before they moved from ga.js to analytics.js recently):

ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

More info: https://developers.google.com/analytics/devguides/collection/gajs/

In new version they used '//' so browser can automatically add protocol:

'//www.google-analytics.com/analytics.js'

So if Google prefers document.location to window.location when they need protocol in JS, I guess they have some reasons for that.

OVERALL: I personally believe that document.location and window.location are the same, but if giant with biggest stats about usage of browsers like Google using document.location, I recommend to follow them.

情深如许 2024-12-18 02:44:05

实际上,我注意到两者之间的 chrome 存在差异,例如,如果您想从子框架导航到沙盒框架,那么您可以仅使用 document.location 而不是使用 window.location 来执行此操作

Actually I notice a difference in chrome between both , For example if you want to do a navigation to a sandboxed frame from a child frame then you can do this just with document.location but not with window.location

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文