Library that monitors changes in URL parameters. Demo
http://www.example.com/index.html#hoge
→http://www.example.com/index.html#fuga
document.addEventListener("changeLocation", e => {
console.log(e.location.hash); //#fuga
});
http://www.example.com/index.html?key1=fuga&key2=piyo
document.addEventListener("changeLocation", e => {
console.log(e.location.parameters); //{key1: "fuga", key2: "piyo"}
});
const eventHandler = e => {
console.log(e.location.hash); //#fuga
console.log("params:", e.location.parameters); //{key1: "fuga", key2: "piyo"}
}
document.addEventListener("changeLocation", eventHandler);
const monitoringLocation = new MonitoringLocation();
monitoringLocation.enable();
monitoringLocation.fireEvent();
You can specify the interval (in milliseconds) for monitoring the URL. The initial value is 100ms.
const monitoringLocation = new MonitoringLocation();
Or
const monitoringLocation = new MonitoringLocation(10);
monitoringLocation.enable();
monitoringLocation.disable();
Use this when you want to retrieve parameters when page is opened.
monitoringLocation.fireEvent();
Event type is "changeLocation"
Listener's argument contains location.
The URL parameter is stored as an object in location.parameters
.
document.addEventListener("changeLocation", eventHandler);
function eventHandler(e){
console.log(e.location.hash); //#fuga
console.log("params:", e.location.parameters); //{key1: "fuga", key2: "piyo"}
}
When parameters and hash are mixed in URL, please set ** parameter first and hash as last **.
Good example
http://www.example.com/index.html?key1=hoge&key2=fuga#hash
Bad example
http://www.example.com/index.html#hash?key1=hoge&key2=fuga