Last active
March 22, 2023 08:28
-
-
Save hasandelibas/12050fc59d675181ea973d21f882081a to your computer and use it in GitHub Desktop.
Js Extended History Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author: Hasan Delibaş | |
Under MIT License | |
*/ | |
// CustomEvent | |
(function () { | |
if ( typeof window.CustomEvent === "function" ) return false; | |
function CustomEvent ( event, params ) { | |
params = params || { bubbles: false, cancelable: false, detail: null }; | |
var evt = document.createEvent( 'CustomEvent' ); | |
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail ); | |
return evt; | |
} | |
window.CustomEvent = CustomEvent; | |
})(); | |
(function(){ | |
let stateSymbol = "__state__index__"; | |
history.stateIndex =-1; | |
history.states=[]; | |
let pushState = history.pushState; | |
function add(data,title,url){ | |
if(data==null) data={}; | |
if(typeof data!="object") data={data:data}; | |
data[stateSymbol] = (history.stateIndex+1); | |
history.states.splice(history.stateIndex+1,0,[data,title,url]) | |
history.states.splice(history.stateIndex+2) | |
history.stateIndex++; | |
} | |
history.pushState =function(data,title,url=null){ | |
add(data,title,url); | |
pushState.bind(history)(data,title,url); | |
} | |
addEventListener("popstate",function(e){ | |
var eventObject= {}; | |
var newStateIndex = e.state!=null ? e.state[stateSymbol] : -1; | |
eventObject.from = history.states[history.stateIndex]; | |
eventObject.to = newStateIndex>-1 ? history.states[newStateIndex] : null; | |
eventObject.side = history.stateIndex>newStateIndex ? "back" : "forward"; | |
if( newStateIndex > -1 && !(newStateIndex in history.states) ){ | |
add(history.state,"",window.location.href); | |
} | |
window.dispatchEvent(new CustomEvent("historyChange", {detail: eventObject} )) | |
history.stateIndex = e.state!=null ? e.state[stateSymbol] : -1; | |
}); | |
})(); | |
/****************************************************************** | |
Using | |
history.states | |
history.stateIndex | |
addEventListener("historyChange",function(e)) | |
******************************************************************/ | |
/** | |
* @param e.detail.from [data,title,url] | |
* @param e.detail.to [data,title,url] | |
* @param e.detail.side "back" | "forward" | |
*/ | |
addEventListener("historyChange",function(e){ | |
var from = e.detail.from; // [ data , title , url ] | |
var to = e.detail.to; // [ data , title , url ] | |
var side = e.detail.side; // "back" | "forward" | |
console.log( `You changed history. Side is ${e.detail.side}.\nFrom:${e.detail.from[2]}\nTo:${e.detail.to[2]}`) | |
}) | |
history.pushState("1", "DENEME-TEST" ,"?1"); | |
history.pushState("2", "DENEME-TEST" ,"?2"); | |
// list of history states | |
console.log( history.states ) | |
/* | |
[ | |
[ {...} , "DENEME-TEST" ,"?1" ] | |
[ {...} , "DENEME-TEST" ,"?2" ] | |
] | |
*/ | |
// get history current state index | |
console.log( history.stateIndex ) | |
/* | |
1 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment