


Use JavaScript to modify the implementation code of the browser URL address bar_javascript skills
There is a very interesting feature in current browsers. You can modify the browser URL without refreshing the page; during the browsing process, you can store the browsing history and click Back when you click on the browser. When pressing the button, you can browse the browsing history to get the rollback information. This does not sound complicated and can be implemented. Let's write some code. Let's see how it works.
var stateObject = {};
var title = "Wow Title";
var newUrl = "/my/awesome/url";
history.pushState(stateObject,title,newUrl);
History object pushState() This method has 3 parameters, as you can see from the example above. The first parameter is a Json object, which stores any historical information about the current URL. The second parameter, title, is equivalent to passing the title of a document, and the third parameter is used to pass the new URL. You will I see that the browser's address bar changes but the current page does not refresh.
Let’s look at an example where we will store some arbitrary data in each individual URL.
for(i=0;i<5;i ) {
var stateObject = {id: i};
var title = "Wow Title " i;
var newUrl = "/my/awesome/url/" i;
history.pushState(stateObject ,title,newUrl);
}
Run it now and click your browser’s back button to see how the URL changes. For each URL change, it stores the historical state "id" and the corresponding value. But how do we recapture the historical state and do something based on it? We need to add an event listener to "popstate", which will be triggered every time the state of the history object changes.
for(i=0;i<5;i ) {
var stateObject = {id: i};
var title = "Wow Title " i;
var newUrl = "/my/awesome/url/" i;
history.pushState(stateObject ,title,newUrl);
alert(i);
}
window.addEventListener('popstate', function(event) {
readState(event.state);
});
function readState(data){
alert(data.id);
}
Now you will see that whenever you click the back button, a "popstate" event will be fired. Our event listener then retrieves the URL the history state object is associated with and prompts for the value of "id".
It's very simple and fun, isn't it?
Original English text: http://hasin.me/2013/10/16/manipulating-url-using-javascript-without-freshing-the-page/

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



The method of customizing resize symbols in CSS is unified with background colors. In daily development, we often encounter situations where we need to customize user interface details, such as adjusting...

Discussion on using custom stylesheets in Safari Today we will discuss a custom stylesheet application problem for Safari browser. Front-end novice...

Using locally installed font files in web pages Recently, I downloaded a free font from the internet and successfully installed it into my system. Now...

How to use JavaScript or CSS to control the top and end of the page in the browser's printing settings. In the browser's printing settings, there is an option to control whether the display is...

How to use locally installed font files on web pages Have you encountered this situation in web page development: you have installed a font on your computer...

Why do negative margins not take effect in some cases? During programming, negative margins in CSS (negative...

The problem of container opening due to excessive omission of text under Flex layout and solutions are used...

Regarding the problem of automatically downloading images when tag links with same origin, many developers will encounter the image after clicking when using tag links with same origin...
