anson920520 anson920520 - 7 months ago 14
Javascript Question

I want the same function react in both way

I have a function which hidden/show a button when the

location.hash()
has been changed

window.addEventListener("hashchange", function() {
//some method to toggle a button
})


but i found that it won't execute when a user refresh the page instead of clicking the link to get in the individual page.

So i tried to put this in a
.ready()
but still

$(document).ready(function() {

window.addEventListener("hashchange", function() {
//some method to toggle a button
})
});


but it doesn't seem like it excute when the user refresh the page

Answer
function toggleButton() {
  // Some method to toggle a button
}

document.addEventListener("DOMContentLoaded", toggleButton);
window.addEventListener("hashchange", toggleButton);
Comments