Skip to content
Fix Code Error

How do I modify the URL without reloading the page?

March 13, 2021 by Code Error
Posted By: Anonymous

Is there a way I can modify the URL of the current page without reloading the page?

I would like to access the portion before the # hash if possible.

I only need to change the portion after the domain, so it’s not like I’m violating cross-domain policies.

 window.location.href = "www.mysite.com/page2.php";  // Sadly this reloads

Solution

This can now be done in Chrome, Safari, Firefox 4+, and Internet Explorer 10pp4+!

See this question’s answer for more information:
Updating address bar with new URL without hash or reloading the page

Example:

 function processAjaxData(response, urlPath){
     document.getElementById("content").innerHTML = response.html;
     document.title = response.pageTitle;
     window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
 }

You can then use window.onpopstate to detect the back/forward button navigation:

window.onpopstate = function(e){
    if(e.state){
        document.getElementById("content").innerHTML = e.state.html;
        document.title = e.state.pageTitle;
    }
};

For a more in-depth look at manipulating browser history, see this MDN article.

Answered By: Anonymous

Related Articles

  • In CSS Flexbox, why are there no "justify-items" and…
  • How do i update a javascript variable as its value changes?
  • Generate a random point within a circle (uniformly)
  • GLYPHICONS - bootstrap icon font hex value
  • Why doesnt my table sort my div variable in numerical order?
  • Setting up EmberJS project directory structure?
  • What does do?
  • How many files can I put in a directory?
  • Drop-down menu that opens up/upward with pure css
  • Remove URL parameters without refreshing page
  • window.history.pushState refreshing the browser
  • What are the currently supported CSS selectors…
  • How do I style a dropdown with only CSS?
  • Simple dynamic breadcrumb
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Excel formula to get ranking position
  • Good way of getting the user's location in Android
  • Can I destructure to multiple variables when at…
  • Running Internet Explorer 6, Internet Explorer 7,…
  • Disabling a button upon condition in Google App script
  • What is causing this broken animation/transition in…
  • How to detect Safari, Chrome, IE, Firefox and Opera browser?
  • Reference - What does this regex mean?
  • Remix error The transaction has been reverted to the…
  • Using `window.location.hash.includes` throws “Object…
  • How to get the current location latitude and…
  • Account for Backbone.js pushState routes with…
  • Why is it that "No HTTP resource was found that…
  • JavaScript TypeError: Cannot read property 'style' of null
  • Is there any "font smoothing" in Google Chrome?
  • Ways to circumvent the same-origin policy
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Javascript validate all checkboxes are selected
  • How to create a game over screen for a basic HTML/JS game?
  • How to detect my browser version and operating…
  • Backbone.js: TypeError: Object # has no method 'parse'
  • Ember link-to throws error / does not clear screen…
  • during wct test: Failed to load resource: the server…
  • How to configure postgresql for the first time?
  • Avoid using document.getElementById with Backbone.js
  • How to implement my own history stack in a single…
  • How to get content from [object HTMLDocument] in javascript
  • Difference between variable declaration syntaxes in…
  • How do I change the URL of the page in Nuxt SSR mode…
  • Using CSS for a fade-in effect on page load
  • SameSite warning Chrome 77
  • How to downgrade from Internet Explorer 11 to…
  • Remove HTML tags from string including &nbsp in C#
  • I'm trying to take inputs from an HTML form and pass…
  • router routes not getting triggered by links
  • How to use standard URL in Backbone.Router?
  • Use latest version of Internet Explorer in the…
  • how to use canvas in JavaScript flappy bird code
  • How to Detect Browser Back Button event - Cross Browser
  • Active tab issue on page load HTML
  • Replacing a 32-bit loop counter with 64-bit…
  • Manually adding a Userscript to Google Chrome
  • Countdown timer goes into negative numbers instead…
  • Using Javascript: How to create a 'Go Back' link…
  • Run chrome in fullscreen mode on Windows
  • HTTP Status 500 - org.apache.jasper.JasperException:…
  • Fundamental difference between Hashing and…
  • Check for special characters in string
  • Loading cross-domain endpoint with AJAX
  • Programmatically navigate using react router V4
  • "Access is denied" JavaScript error when trying to…
  • Cannot make table responsive
  • showing record text with red color when click stop button
  • Programmatically navigate using React router
  • Backbone.js PushStates: Fallback for Internet…
  • How to transfer browser’s session state across pages?
  • How to detect chrome and safari browser (webkit)
  • Backbone.js: workaround for implementing slash based…
  • can binding work when using .template instead of…
  • How can I center all my contents in html?
  • Ukkonen's suffix tree algorithm in plain English
  • Displaying the Dates that user have chosen from date input
  • Smart way to truncate long strings
  • Simple pagination in javascript
  • How should a model be structured in MVC?
  • Setting DIV width and height in JavaScript
  • XMLHttpRequest cannot load ✘✘✘ No…
  • How to use Servlets and Ajax?
  • javascript minesweeper issue with revealing tiles…
  • Selenium using Java - The path to the driver…
  • I made a game it should display two images when i…
  • SecurityException: Permission denied (missing…
  • Vue.js dynamically appending HTML when clicking on a button
  • Use of PUT vs PATCH methods in REST API real life scenarios
  • highcharts will not render to my div
  • Detect Route Change with react-router
  • Can a Backbone.js app be progressively enhanced, and…
  • Polymer paper-dialog: how can I know when an…
  • Polymer: how to change a parent's property from a child?
  • How to get current location in Android
  • Is it possible to apply CSS to half of a character?
  • trying to update parent from child emit in vue 2
  • How to make the overflow CSS property work with…
  • JS file is not updating to new id values
  • How to add a regular list view in the extended part…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I find the length of an array?

Next Post:

Create ArrayList from array

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error