document.location.href和document.location有什么区别? [英] What is the difference between document.location.href and document.location?

查看:177
本文介绍了document.location.href和document.location有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

document.location.href document.location

浏览器是否相同?

推荐答案

document.location window.location 的同义词,几乎与JavaScript一直存在时已被弃用。不要使用它。

document.location is a synonym for window.location that has been deprecated for almost as long as JavaScript has existed. Don't use it.

location 是一个结构化对象,其属性对应于URL的各个部分。 location.href 是单个字符串中的整个URL。将字符串分配给任一字符串定义为导致相同类型的导航,因此请选择。

location is a structured object, with properties corresponding to the parts of the URL. location.href is the whole URL in a single string. Assigning a string to either is defined to cause the same kind of navigation, so take your pick.

我考虑写入 location.href = something 略微好一点,因为它稍微明确一下它在做什么。你通常想避免只是 location = something ,因为它看起来像一个变量赋值的误导。 window.location =某事虽然没问题。

I consider writing to location.href = something to be marginally better as it's slightly more explicit about what it's doing. You generally want to avoid just location = something as it looks misleadingly like a variable assignment. window.location = something is fine though.

这篇关于document.location.href和document.location有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆