一千萬個為什麽

搜索

document.location.href和document.location有什麽區別?

document.location.hrefdocument.location 之間有什麽區別?

跨瀏覽器是否相同?

最佳答案

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 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 = something 雖然很好。

轉載註明原文: document.location.href和document.location有什麽區別?