Home Web Front-end JS Tutorial pushState+Ajax realizes page switching without refreshing

pushState+Ajax realizes page switching without refreshing

Apr 03, 2018 am 10:08 AM
switch page

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>
Copy after login

Core code

  function setUrl(page){
    var url = location.pathname + '?page=' + page;
    history.pushState({
      url : url
    },'',url);
  }
//每次点击按钮的时候就往历史记录里面增加一个条目
Copy after login

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); 
  })()
Copy after login

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);
  })
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to copy a page in Word How to copy a page in Word Feb 20, 2024 am 10:09 AM

How to copy a page in Word

Operation tutorial for switching from win11 home version to professional version_Operation tutorial for switching from win11 home version to professional version Operation tutorial for switching from win11 home version to professional version_Operation tutorial for switching from win11 home version to professional version Mar 20, 2024 pm 01:58 PM

Operation tutorial for switching from win11 home version to professional version_Operation tutorial for switching from win11 home version to professional version

How to switch between 4g and 5g on Xiaomi Mi 14Ultra? How to switch between 4g and 5g on Xiaomi Mi 14Ultra? Feb 23, 2024 am 11:49 AM

How to switch between 4g and 5g on Xiaomi Mi 14Ultra?

How to implement dual system switching in Win10 system How to implement dual system switching in Win10 system Jan 03, 2024 pm 05:41 PM

How to implement dual system switching in Win10 system

Switch the dual system boot mode of Apple computer Switch the dual system boot mode of Apple computer Feb 19, 2024 pm 06:50 PM

Switch the dual system boot mode of Apple computer

How to deal with the problem that Laravel page cannot display CSS correctly How to deal with the problem that Laravel page cannot display CSS correctly Mar 10, 2024 am 11:33 AM

How to deal with the problem that Laravel page cannot display CSS correctly

How to customize and edit standby mode on iPhone: What's new in iOS 17 How to customize and edit standby mode on iPhone: What's new in iOS 17 Sep 21, 2023 pm 04:01 PM

How to customize and edit standby mode on iPhone: What's new in iOS 17

How to quickly refresh a web page? How to quickly refresh a web page? Feb 18, 2024 pm 01:14 PM

How to quickly refresh a web page?

See all articles