I don’t think the whole Facebook page is loaded. Each link has its own "target". Most of them bring a page (I think it's just AJAX) to show, others just change some parts of the screen. So let's say you have two divs. One div is chat-div. The positioning is fixed and everything, z-index at 100, it will always remain on top. The rest of the page is another div. Inside this div, you can load specific pages using AJAX, without refreshing the entire screen.
As with screen reloading: you can easily save (also with AJAX) whether the user closed the chat screen or opened it. Just create a table in the database called “chats” or something else, and then when the chat screen opens, you put an entry in this table with “person_1”, “person_2”, “lastmessage" and "active". When they close the chat, you can put the "active" field in false. Then, whenever someone downloads the entire site, you check the chats for active chats and show them when they are.
source share