Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset focus on Vue SPA page

I'm developping Vue SPA with Vue router app and I'm trying to simulate oage reload, by resetting focus state after changing the route. Here is my code:

router.beforeEach((to, from, next) => {
  document.activeElement.blur();
  next();
});

My problem is that focus indeed returns to body element right after change, but on next focus it goes back to where it was, in this case footer, instead of "skip to content" button. I tried setting focus to window explicitly, but it doesn't work either. Does anyone have an idea how to reset focus flow for real, so it truly starts over? Any help will be appreciated!

like image 491
p-sara Avatar asked Dec 06 '25 10:12

p-sara


1 Answers

Basing on this article I added tabindex -1 right before and removed it right after. It did the trick

router.beforeEach((to, from, next) => {
  document.body.setAttribute("tabindex", "-1");
  document.body.focus();
  document.body.removeAttribute("tabindex");
  next();
});
like image 113
p-sara Avatar answered Dec 08 '25 23:12

p-sara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!