頁面實現刷新和跳轉

1. 重新加載本頁


	window.location.reload()
	window.location.replace()

這兩種方法都可以重新加載本頁,但是replace()可以導向另外一個URL
例如:window.location.replace(“http://www.baidu.com”)

2. 跳轉歷史頁


	window.history.back();
	window.history.go(-1);

back和go裏面都可以放數值

例如:裏面放上 -1 意思就是返回上一級,裏面放上 -2 就是返回上上級,以此類推
而且裏面還可以放上指定的路由路徑,比如 window.history.go(’…/routes/admin/’); 這樣可以跳轉到指定的路由模塊

3. 跳轉目標頁面


window.location(.href)="URL"
/*
	其實 .href 可以省略
	window.location 和 window.location.href 實現的效果是一樣的
	
	// e.g
	window.location = "http://www.baidu.com"
	window.location.href = http://www.baidu.com
	
	上面兩種方法都可以從當前頁面跳轉到目標頁面
	不同之處在於 window.location 返回的是對象,如果沒有.href,它會默認參數就是href
*/


winodw.location.assign("URL")
/*
	這個方法和上面的方法差不多
	用法:	
	winodw.location.assign(http://www.baidu.com)
*/


window.location.replace("URL")
/*
	
	如果用window.location.replace("http://www.baidu.com") 實現跳轉頁面, 它和上面方法的區別在於它跳轉後不會保存跳出頁面的信息.
	
	所以如果使用 history 歷史返回按鈕是無效的
	
	它的效果類似於在網頁上點擊右鍵, 在新窗口打開或新標籤頁打開.
	
	而 window.location.href 實現的效果是在頁面上點擊目標鏈接, 然後可以點擊歷史返回按鈕, 返回到之前頁面。
*/

4. self、parent、this、top


	top.location.href=”url”  	 	// 在頂層頁面打開url(跳出框架) 
	self.location.href=”url”  		// 僅在本頁面打開url地址 
	parent.location.href=”url”  	// 在父窗口打開Url地址 
	this.location.href=”url”  		// 用法和self的用法一致
	

if (top.location == self.location) 判斷當前location是否爲頂層 來禁止frame引用,如果頁面當中有自定義的frame的話,也可以將parent self top換爲自定義frame的名稱 ,效果就是在自定義frame窗口打開url

5. jquery方法


	$(location).attr('href', '//www.jb51.net');
	 
	$(window).attr('location','//www.jb51.net');
	 
	$(location).prop('href', '//www.jb51.net')
	
發佈了15 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章