hashchange事件的認識

2022-07-25 02:15:11 字數 621 閱讀 5654

寫過路由的同學都知道其原理是通過url的改變,由導航資訊來決定頁面資訊。。。表述的好像有點不準確

這麼說吧,只要url引數列表只要變化就會呼叫hashchange事件,此時event物件包括oldurl和newurl兩個屬性,分別儲存引數列表變化前後的完整url。

目前支援hashchange事件的瀏覽器有ie8+、firfox3.6、safari5+、chrome和opera10.6+,其中只有firfox6+、chrome和opera支援oldurl和newurl屬性,

因此,最好使用location物件來確定當前的引數列表:

eventutil.addhandler(window,"hashchange",function(event){

console.log(event.oldurl);  

console.log(event.newurl);  

console.log(location.hash);

附:檢測瀏覽器是否支援hashchange事件:

var issupported=("onhashchange" in window) && (document.documentmode === undefined || document.documentmode > 7);

js 中hash和hashchange事件

1 hash即url中 字元後面的部分。使用瀏覽器訪問網頁時,如果網頁url中帶有hash,頁面就會定位到id 或name 與hash值一樣的元素的位置 hash值瀏覽器是不會隨請求傳送到伺服器端的 通過window.location.hash屬性獲取和設定hash值。window.location...

認識js 事件

1 冒泡事件是由微軟公司提出的,從觸發底部事件一直延續到最外層,這個現象就是冒泡!2 只有巢狀關係的標籤和都是一樣的事件才可能產生冒泡!1 evt.stoppropagation 無法阻止 ie 低版本事件冒泡 2 evt.cancelbubble true 相容 ie 低版本冒泡 if evt.c...

事件處理程式的認識

html事件處理程式 dianjifunction clickme event 缺點 dom0級事件處理程式 js處理程式的傳統方式,將乙個函式複製給乙個事件處理程式屬性 var btn document.getelementbyid btn1 btn.onclick function event ...