pushState+Ajax realizes page switching without refreshing
This time I will bring you pushState+Ajax to achieve page switching without refreshing, and pushState+Ajax to achieve page switching without refreshing. What are the things to note?The following is a practical case, let's take a look.
Preface
Such requirements are very common: click on the page number to partially update the page (not refresh as a whole), and generate historical management. Partial refresh is easy to implement. Ajax can meet our needs, but this does not produce historical management. Fortunately, HTML5 provides us with several useful APIs to solve this problem, see below.
Text
1. API
1. pushState
pushState () takes three parameters: a state object, a title (now ignored), and an optional URL address.
state: State information corresponding to the URL to be jumped to.
title: Empty String (may be useful in the future).
url: URL address to jump to, cannot cross domain.
Function: Add the current URL and history.state to history, and replace the current one with the new state and URL. It will not cause the page to refresh.
2. replaceState
The history.replaceState() operation is similar to history.pushState(), except that the replaceState() method will modify the current history entry instead of creating a new entry.
3, window.onpopstate
history.go and history.back (including the user pressing the browser history forward and back buttons) are triggered, and the page is not refreshed (due to the use of pushState to modify the history ) will trigger the popstate event. When the event occurs, the browser will take out the URL and the corresponding state object from the history to replace the current URL and history.state. History.state can also be obtained through event.state.
2. It is very simple to realize the
scenario. Different pictures and titles will appear in the p below the button. Use ajax to refresh and generate history management.
<p class="page"> <a href="javascript:;">[ <span>1</span> ]</a> <a href="javascript:;">[ <span>2</span> ]</a> <a href="javascript:;">[ <span>3</span> ]</a> <a href="javascript:;">[ <span>4</span> ]</a> </p> <p> <img /> <p class="title"></p> </p>
Core code
function setUrl(page){ var url = location.pathname + '?page=' + page; history.pushState({ url : url },'',url); } //每次点击按钮的时候就往历史记录里面增加一个条目
Note that when loading for the first time, you need to load and replace the history record
(function(){ //默认显示第一页 var url = location.pathname + '?page=1'; //修改当前的历史记录 history.replaceState({ url : url },'',url); })()
Listen to the popstage event of the window and get the current history when the event occurs Corresponding page number, and then execute ajax
window.addEventListener('popstate',function(){ var page = getPage(); xhr(page); })
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
How to implement AJAX request array
How to clear the cache in Ajax
The above is the detailed content of pushState+Ajax realizes page switching without refreshing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Want to copy a page in Microsoft Word and keep the formatting intact? This is a smart idea because duplicating pages in Word can be a useful time-saving technique when you want to create multiple copies of a specific document layout or format. This guide will walk you through the step-by-step process of copying pages in Word, whether you are creating a template or copying a specific page in a document. These simple instructions are designed to help you easily recreate your page without having to start from scratch. Why copy pages in Microsoft Word? There are several reasons why copying pages in Word is very beneficial: When you have a document with a specific layout or format that you want to copy. Unlike recreating the entire page from scratch

Xiaomi 14Ultra is one of the most popular Xiaomi models this year. Xiaomi 14Ultra not only upgrades the processor and various configurations, but also brings many new functional applications to users. This can be seen from the sales of Xiaomi 14Ultra mobile phones. It is very popular, but there are some commonly used functions that you may not know yet. So how does Xiaomi 14Ultra switch between 4g and 5g? Let me introduce the specific content to you below! How to switch between 4g and 5g on Xiaomi 14Ultra? 1. Open the settings menu of your phone. 2. Find and select the "Network" and "Mobile Network" options in the settings menu. 3. In the mobile network settings, you will see the "Preferred network type" option. 4. Click or select this option and you will see

How to convert Win11 Home Edition to Win11 Professional Edition? In Win11 system, it is divided into Home Edition, Professional Edition, Enterprise Edition, etc., and most Win11 notebooks are pre-installed with Win11 Home Edition system. Today, the editor will show you the steps to switch from win11 home version to professional version! 1. First, right-click on this computer on the win11 desktop and properties. 2. Click Change Product Key or Upgrade Windows. 3. Then click Change Product Key after entering. 4. Enter the activation key: 8G7XN-V7YWC-W8RPC-V73KB-YWRDB and select Next. 5. Then it will prompt success, so you can upgrade win11 home version to win11 professional version.

Many friends may not be used to the win system when they first come into contact with it. There are dual systems in the computer. At this time, you can actually switch between the two systems. Let's take a look at the detailed steps for switching between the two systems. How to switch between two systems in win10 system 1. Shortcut key switching 1. Press the "win" + "R" keys to open Run 2. Enter "msconfig" in the run box and click "OK" 3. In the open "System Configuration" In the interface, select the system you need and click "Set as Default". After completion, "Restart" can complete the switch. Method 2. Select switch when booting 1. When you have dual systems, a selection operation interface will appear when booting. You can use the keyboard " Up and down keys to select the system

How to switch between Apple dual systems when starting up Apple computers are powerful devices. In addition to their own macOS operating system, you can also choose to install other operating systems, such as Windows, to achieve dual system switching. So how do we switch between the two systems when booting? This article will introduce to you how to switch between dual systems on Apple computers. First of all, before installing dual systems, we need to confirm whether our Apple computer supports dual system switching. Generally speaking, Apple computers are based on

Page refresh is very common in our daily network use. When we visit a web page, we sometimes encounter some problems, such as the web page not loading or displaying abnormally, etc. At this time, we usually choose to refresh the page to solve the problem, so how to refresh the page quickly? Let’s discuss the shortcut keys for page refresh. The page refresh shortcut key is a method to quickly refresh the current web page through keyboard operations. In different operating systems and browsers, the shortcut keys for page refresh may be different. Below we use the common W

Standby is a new feature in the iOS 17 update that provides a new and enhanced way to access information when your phone is idle quickly. With StandBy, you can conveniently check the time, view upcoming events, browse your calendar, get weather updates for your location, and more. Once activated, the iPhone will intuitively enter standby mode when set to landscape while charging. This feature is perfect for wireless charging points like your bedside table, or when you're away from your iPhone charging during daily tasks. It allows you to swipe through various widgets displayed in standby to access different sets of information from various applications. However, you may want to modify these widgets or even delete some based on your preferences and the information you need frequently. So let's dive into

In the application of excel software, we are accustomed to using shortcut keys to make some operations easier and faster. Sometimes there is related data between multiple tables in excel. When we view it, we have to constantly switch between tasks. If there is a faster switching method, it will save a lot of wasted time on switching, which will greatly help improve work efficiency. What method can be used to complete quick switching? To address this issue, the editor will talk about it today The content is: How to use the shortcut keys for switching workbooks in Excel. 1. First, you can see multiple workbooks at the bottom of the open excel table. You need to quickly switch between different workbooks, as shown in the figure below. 2. Then press the Ctrl key on the keyboard without moving, and select the job to the right if you need to
