Home >Web Front-end >JS Tutorial >How Can I Change a URL Without Reloading the Page?

How Can I Change a URL Without Reloading the Page?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-21 17:29:10913browse

How Can I Change a URL Without Reloading the Page?

Modifying the URL Without Page Reload

Problem:

Is it possible to alter the URL of the current page without triggering a page refresh?

Objective:

Access and modify the portion of the URL after the domain name, excluding the hash (#).

Solution:

Window.history.pushState()

Modern browsers support the pushState() method, which allows you to manipulate the browser's history without reloading the page.

Implementation:

  1. Update the content of the page (e.g., through AJAX).
  2. Set the URL path using window.history.pushState() and the desired urlPath.
function processAjaxData(response, urlPath){
     // ... (update content)

     window.history.pushState({
         "html": response.html,

The above is the detailed content of How Can I Change a URL Without Reloading the Page?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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