Difference between [removed] and location.href

前端 未结 6 1421
一个人的身影
一个人的身影 2020-12-01 07:35

I am confused as to the difference between window.location and location.href. Both appear to be acting in the same way.

What is the differe

6条回答
  •  天涯浪人
    2020-12-01 07:55

    They are different. window.location is an object containing the property href which is a string.

    Setting window.location and window.location.href behave the same way, as you noticed, because it was built into the JavaScript language long ago. Read more in this question about setting window.location.

    Getting window.location and window.location.href behave differently because the former is an object and the latter is a string. If you run string functions like indexOf() or toLowerCase(), you have to use window.location.href.

提交回复
热议问题