How does the Back button in a web browser work?

前端 未结 8 1162
北恋
北恋 2020-11-28 02:57

I searched the Web about this question but I found nothing:

What is the logic of the back button? What is happening when we hit the back button on a Web browser?

8条回答
  •  一生所求
    2020-11-28 03:00

    I think the easiest way to explain this is in pseudocode:

    class Page:
        String url, ...
        Page previous, next # implements a doubly-linked list
    
    class History:
        Page current # current page
    
        void back():
            if current.previous == null:
                return
            current = current.previous
            refresh()
    
        void forward():
            if current.next == null:
                return
            current = current.next
            refresh()
    
        void loadPage(Page newPage):
            newPage.previous = current
            current.next = newPage # remove all the future pages
            current = current.next
            display(current)
    

提交回复
热议问题