JavaScript中的window.open()與window.location.href有什麼不一樣?

window.open(‘index.html’)

表示新增一個窗口打開 index.html 這個頁面,並不刷新。

window.location.href(‘index.html’)

表示在當前窗口重定向到新頁面,打開並刷新 index.html 這個頁面。

window.location 是 window 對象的屬性,用來替換當前頁,也就是重新定位當前頁。

window.open 是 window 對象的方法,是用來打開一個新窗口的函數。

// 打開新頁面
	// 注意:有些瀏覽器的安全設置會將window.open()屏蔽,例如避免彈出廣告窗
	window.open('./index.html');

	// 在原窗口打開新頁面
	window.location.href="./index.html";
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章