1. 程式人生 > >javascript 開啟頁面window.location和window.open的區別

javascript 開啟頁面window.location和window.open的區別

轉自:http://www.jb51.net/article/22616.htm

有時候需要用js來實現頁面的開啟,因為js下有window.location和window.open的不同實現方法,下面來簡單的說明下區別。

window.location = "http://www.xxxxxxxx.net"   跳轉後有後退功能
其實應該是  window.location.href

window.location.replace("http://www.xxxxxxxx.net") 跳轉後沒有後退功能

window.open("http://www.xxxxxxxx.net")   要新的視窗開啟連結

這個一般用於簡單的彈出頁面,現在基本上都被遮蔽掉了 

1.window.location是window物件的屬性,而window.open是window物件的方法 
  window.location是你對當前瀏覽器視窗的URL地址物件的參考!   
  window.open是用來開啟一個新視窗的函式! 

【總結】
1、window.open("**")是用新視窗開啟URL頁面

2、location.href="**"是用當前頁面顯示URL

The Window.location read-only property returns a Location object with
information about the current location of the document.

Though Window.location is a read-only Location object, you can also
assign a DOMString to it. This means that you can work with location
as if it were a string in most cases
location = 'http://www.example.com' is a synonym oflocation.href = 'http://www.example.com'.

https://developer.mozilla.org/en-US/docs/Web/API/Window/location

location 是 location.href 的簡寫,無論是訪問  還是賦值。
從功能上,location 等於 location.href
但從本體論上,location 是一個物件,location.href 是它的一個屬性。
這種怪異的行為應該是為了相容無疑