Inline JS function not defined if loaded through router

Hello
I have a page with inline JS
When I load the page initially, every thing works okay.
When I go to the page through routing, the console returns error “function is not defined” although when I check the dom the function is there.
I tried putting the function in app.js but I need some PHP parsing in there, and these function wouldn’t be needed elsewhere.
Any solutions?
Best regards