1

I want to fire a function every time I change a page in Liferay.

Liferay.on('endNavigate', function(event) {
    functionToExecuteOnEveryPage();
});

It works, but every time I change the page the event 'endNavigate' is invoked multiple times.

enter image description here The first time I change the page the function is executed once, and the second time I change the page the function is executed two times, and the third time I change the page the function is executed three times, and so on..

How can I prevent this behavior?

Mustapha Aoussar
  • 5,833
  • 15
  • 62
  • 107
  • Maybe it's better your functionToExecuteOnEveryPage() is ready for this scenario.. – Daniele Baggio Jan 31 '20 at 12:13
  • you can use `beforeNavigate` method as well.which type of logic you want to perform? – pratik patel Jan 31 '20 at 12:30
  • 1
    @pratikpatel `beforeNavigate` fires before navigation starts. I want to execute my function after the page is fully loaded that's why I use `endNavigate`. – Mustapha Aoussar Jan 31 '20 at 15:41
  • 1
    Your problem is already defined in Liferay documentation. You can detach the event listener. This [link](https://portal.liferay.dev/docs/7-1/tutorials/-/knowledge_base/t/automatic-single-page-applications#detaching-global-listeners) may help you. – pratik patel Feb 01 '20 at 04:13
  • @pratikpatel Can you post your answer with an example? – Mustapha Aoussar Feb 04 '20 at 12:00

0 Answers0