is there a page back event or something similar to it?

is there a page back event or something similar to it?

I dynamically create a page. what i want is that when people navigate to another page, this page stays in the dom, so people can go back to it from the other pages. so i set data-dom-cache="true".

but when people move back the the page before it, i want to remove the div from the dom. here is an example

page1 -> page2 -> page3

i dynamically create page2. on page3, the div for page2 is in the dom. but returning to page1, i want to remove the page2 div. is that possible? or do i need to track all the navigation myself. because the piece of code (a javascript function) that creates and displays page2 knows nothing about page1 and page3, so it can be used anywhere. so is there a generic way of accomplish what i need?

thanks for your input