


Use ajax to change page content and address bar URL without refreshing
This article mainly introduces in detail the method of using ajax and window.history.pushState to change the page content and address bar URL without refreshing. Friends who need it can refer to it
When visiting the now very popular Google Plus, Careful users may find that clicks between pages are requested asynchronously through ajax, and the URL of the page changes at the same time. And it can support the browser's forward and backward very well. People can't help but wonder, what has such a powerful function?
HTML5 references new APIs, namely history.pushState and history.replaceState. It is through this interface that the page URL can be changed without refreshing.
Differences from traditional AJAX
Traditional ajax has the following problems:
Although ajax can change the page content without refreshing, it cannot be changed Page URL
Secondly, for better accessibility, change the hash of the URL after the content changes. However, the hash method cannot handle the browser's forward and backward issues well
Some browsers have introduced the onhashchange interface, and browsers that do not support it can only periodically determine whether the hash has changed
Furthermore, the use of ajax is very unfriendly to search engines, and the areas crawled by spiders are often empty
In order to solve the problems caused by traditional ajax, a new API has been introduced in HTML5, namely: history .pushState, history.replaceState
You can operate the browser history through the pushState and replaceState interfaces and change the URL of the current page.
pushState is to add the specified URL to the browser history, and replaceState is to replace the current URL with the specified URL.
How to call
var state = { title: title, url: options.url, otherkey: othervalue }; window.history.pushState(state, document.title, url);
In addition to title and url, the state object can also add other data, for example: you also want to save some configurations for sending ajax stand up.
replaceState and pushState are similar and require no further explanation.
How to respond to the browser's forward and backward operations
The onpopstate event is provided on the window object. The state object passed above will become a sub-object of the event, so that the stored title and URL.
window.addEventListener('popstate', function(e){ if (history.state){ var state = e.state; //do something(state.url, state.title); } }, false);
In this way, you can combine ajax and pushState for perfect browsing without refreshing.
Some restrictions
1. It cannot cross domain. This is inevitable. To quote a classic saying I once saw on the Internet: "If javascript can cross domain, then it can be incredible!"
2. Although the state object can store many custom attributes, the value cannot is an object.
Corresponding to some back-end processing
In this mode, in addition to currently using ajax to browse without refreshing, it is also necessary to ensure that normal browsing can also be performed after directly requesting the changed URL. , so the backend needs to handle these.
1. A special header can be sent to ajax combined with pushState, such as: setRequestHeader(‘PJAX’, ‘true’).
2. When the backend obtains the header with PJAX=true, the common parts of the page will not be output. For example: PHP can make the following judgment
function is_pjax(){ return array_key_exists('HTTP_X_PJAX', $_SERVER) && $_SERVER['HTTP_X_PJAX'] === 'true'; }
Although the interface only has pushState, replaceState, and onpopstate, a lot of processing still needs to be done when using it.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to solve the problem of ajax failure on Google Chrome browser
Based on ajax to realize click loading and more no refresh loading Go to this page
Two solutions to Ajax cross-domain problems
The above is the detailed content of Use ajax to change page content and address bar URL 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

AI Hentai Generator
Generate AI Hentai for free.

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



When you browse the web on your iPhone, the loaded content is temporarily stored as long as the browser app remains open. However, the website updates content regularly, so refreshing the page is an effective way to clear out old data and see the latest published content. This way, you always have the latest information and experiences. If you want to refresh the page on iPhone, the following post will explain you all the methods. How to Refresh Web Pages on Safari [4 Methods] There are several methods to refresh the pages you are viewing on the Safari App on iPhone. Method 1: Use the Refresh Button The easiest way to refresh a page you have open on Safari is to use the Refresh option on your browser's tab bar. If Safa

Is the F5 key not working properly on your Windows 11/10 PC? The F5 key is typically used to refresh the desktop or explorer or reload a web page. However, some of our readers have reported that the F5 key is refreshing their computers and not working properly. How to enable F5 refresh in Windows 11? To refresh your Windows PC, just press the F5 key. On some laptops or desktops, you may need to press the Fn+F5 key combination to complete the refresh operation. Why doesn't F5 refresh work? If pressing the F5 key fails to refresh your computer or you are experiencing issues on Windows 11/10, it may be due to the function keys being locked. Other potential causes include the keyboard or F5 key

In the operation process of the Meituan Takeout Merchant Edition, the setting of the starting delivery price is a crucial link. A reasonable starting delivery price can not only help merchants control costs, but also increase order amounts to a certain extent, thus increasing overall revenue. However, many merchants don’t know much about how to modify the minimum delivery price. So in the following article, the editor of this website will bring you detailed starting price setting guide for merchants. If you want to know more, come to the following article to find out! In the Meituan Takeout Merchant Center, log in and enter the store settings, then select store management. In the switch navigation at the top of the store management page, select delivery information, and then click Add Delivery Area to complete the operation. Once you add a location, the corresponding shipping costs will automatically appear. After completing your order, you will receive

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

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

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:
