
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
html5-history-api
Advanced tools
HTML5 History API expansion for browsers not supporting pushState, replaceState
This Javascript library provides an emulation of HTML5 History API for older browsers.
The library operates according to W3C specification, adding no new or incompatible methods. The library can be used exactly as described, for example, in Dive Into HTML5 book (http://diveintohtml5.info/history.html) or in the History API Specification (http://www.w3.org/TR/html5/browsers.html#the-history-interface).
npm install html5-history-api
history.js
- IE8+ and other browsers
history.ielte7.js
- IE6+ and other browsers
To enable support for HTML5-History-API polyfill in your library, you need to add one line of code:
var location = window.history.location || window.location;
code of library looks like this:
(function(){
// To enable support for HTML5-History-API polyfill in your library
var location = window.history.location || window.location;
// you library code here
// ....
// ....
// ....
})();
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/require.js"></script>
<script type="text/javascript">
requirejs(['/history'], function() {
if (history.emulate) {
console.log('In your browser is emulated HTML5-History-API');
} else {
console.log('In your browser is natively support HTML5-History-API');
}
});
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="history.js"></script>
<script type="text/javascript">
(function(eventInfo) {
// we get a normal Location object
/*
* Note, this is the only difference when using this library,
* because the object window.location cannot be overriden,
* so library the returns generated "location" object within
* an object window.history, so get it out of "history.location".
* For browsers supporting "history.pushState" get generated
* object "location" with the usual "window.location".
*/
var location = window.history.location || window.location;
// hang on the event, all references in this document
document[eventInfo[0]](eventInfo[1] + 'click', function(event) {
event = event || window.event;
var target = event.target || event.srcElement;
// looking for all the links with 'ajax' class found
if (target && target.nodeName === 'A' &&
(' ' + target.className + ' ').indexOf(' ajax ') >= 0)
{
// keep the link in the browser history
history.pushState(null, null, target.href);
// here can cause data loading, etc.
// do not give a default action
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
}
}, false);
// hang on popstate event triggered by pressing back/forward in browser
window[eventInfo[0]](eventInfo[1] + 'popstate', function(event) {
// here can cause data loading, etc.
// just post
alert("We returned to the page with a link: " + location.href);
}, false);
})(window.addEventListener ? ['addEventListener', ''] : ['attachEvent', 'on']);
</script>
</head>
<body>
<a class="ajax" href="/mylink.html">My Link</a>
<a class="ajax" href="/otherlink.html">Other Link</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="history.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function() {
// we get a normal Location object
/*
* Note, this is the only difference when using this library,
* because the object window.location cannot be overriden,
* so library the returns generated "location" object within
* an object window.history, so get it out of "history.location".
* For browsers supporting "history.pushState" get generated
* object "location" with the usual "window.location".
*/
var location = window.history.location || window.location;
// looking for all the links and hang on the event, all references in this document
$(document).on('click', 'a.ajax', function() {
// keep the link in the browser history
history.pushState(null, null, this.href);
// here can cause data loading, etc.
// do not give a default action
return false;
});
// hang on popstate event triggered by pressing back/forward in browser
$(window).on('popstate', function(e) {
// here can cause data loading, etc.
// just post
alert("We returned to the page with a link: " + location.href);
});
});
</script>
</head>
<body>
<a class="ajax" href="/mylink.html">My Link</a>
<a class="ajax" href="/otherlink.html">Other Link</a>
</body>
</html>
history.js?basepath=/pathtosite/ - the base path to the site; defaults to the root "/".
history.js?redirect=true - enable link translation.
history.js?type=/ - substitute the string after the anchor; by default "/".
You can also combine options:
history.js?type=/&redirect=true&basepath=/pathtosite/ - the order of options does not matter.
Or execute special method in JavaScript:
history.redirect(/* type = */ '/', /* basepath = */ '/pathtosite/');
Demo Site: http://history.spb-piksel.ru/ or http://devote.github.io/demos/history/
Follow me on Twitter: https://twitter.com/DimaPakhtinov
ΠΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠ° ΡΠΌΡΠ»ΠΈΡΡΠ΅Ρ HTML5 History API Π² ΡΡΠ°ΡΡΡ Π±ΡΠ°ΡΠ·Π΅ΡΠ°Ρ .
ΠΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠ°, ΠΊΠΎΡΠΎΡΠ°Ρ Π½Π΅ Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅Ρ Π½Π΅Π½ΡΠΆΠ½ΡΠ΅ ΠΌΠ΅ΡΠΎΠ΄Ρ, Π·Π°ΡΡΠ°Π²Π»ΡΡ ΠΈΡ ΠΈΠ·ΡΡΠ°ΡΡ, Π° ΠΎΠΏΠ΅ΡΠΈΡΡΠ΅Ρ ΠΏΠΎ ΡΠΏΠ΅ΡΠΈΡΠΈΠΊΠ°ΡΠΈΠΈ w3c, ΠΏΠΎ ΠΈΠ½ΡΠ΅ΡΡΠ΅ΠΉΡΡ History.
ΠΠ»Ρ ΠΏΡΠΈΠΌΠ΅ΡΠ° ΠΌΠΎΠ³Ρ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊΠΎΡΠΎΡΠΊΠΈΠΉ ΠΊΠΎΠ΄ ΠΊΠ°ΠΊ Ρ Π½Π΅ΠΉ ΡΠ°Π±ΠΎΡΠ°ΡΡ.
ΠΠΎ ΠΏΡΠΈΠ½ΡΠΈΠΏΡ ΠΌΡ ΡΠ°Π±ΠΎΡΠ°Π΅ΠΌ Ρ HTML5 History API ΡΠ°ΠΊ ΠΊΠ°ΠΊ ΠΎΠΏΠΈΡΠ°Π½ΠΎ, Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ, ΡΡΡ http://htmlbook.ru/html5/history ΠΈΠ»ΠΈ ΠΏΠΎ ΡΠΏΠ΅ΡΠΈΡΠΈΠΊΠ°ΡΠΈΠΈ http://www.w3.org/TR/html5/history.html#the-history-interface
npm install html5-history-api
history.js
- IE8+ ΠΈ Π΄ΡΡΠ³ΠΈΠ΅ Π±ΡΠ°ΡΠ·Π΅ΡΡ
history.ielte7.js
- IE6+ ΠΈ Π΄ΡΡΠ³ΠΈΠ΅ Π±ΡΠ°ΡΠ·Π΅ΡΡ
ΠΠ»Ρ Π²ΠΊΠ»ΡΡΠ΅Π½ΠΈΡ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΊΠΈ ΠΏΠ»Π°Π³ΠΈΠ½Π° HTML5-History-API polyfill Π² ΡΠ²ΠΎΠΈΡ Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠ°Ρ , Π΄ΠΎΠ±Π°Π²ΡΡΠ΅ ΡΡΡΠΎΠΊΡ ΠΊΠΎΠ΄Π°:
var location = window.history.location || window.location;
ΠΊΠΎΠ΄ Π±ΡΠ΄Π΅Ρ Π²ΡΠ³Π»ΡΠ΄Π΅ΡΡ ΠΏΡΠΈΠΌΠ΅ΡΠ½ΠΎ ΡΠ°ΠΊ:
(function(){
// ΠΠΊΠ»ΡΡΠ°Π΅Ρ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΊΡ ΠΏΠ»Π°Π³ΠΈΠ½Π° HTML5-History-API polyfill
var location = window.history.location || window.location;
// ΠΊΠΎΠ΄ Π²Π°ΡΠ΅ΠΉ Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠΈ
// ....
// ....
// ....
})();
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/require.js"></script>
<script type="text/javascript">
requirejs(['/history'], function() {
if (history.emulate) {
console.log('Π Π²Π°ΡΠ΅ΠΌ Π±ΡΠ°ΡΠ·Π΅ΡΠ΅ ΡΠΌΡΠ»ΠΈΡΡΠ΅ΡΡΡ HTML5-History-API');
} else {
console.log('Π Π²Π°ΡΠ΅ΠΌ Π±ΡΠ°ΡΠ·Π΅ΡΠ΅ Π΅ΡΡΡ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΊΠ° HTML5-History-API');
}
});
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="history.js"></script>
<script type="text/javascript">
(function(eventInfo) {
// ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ Π½ΠΎΡΠΌΠ°Π»ΡΠ½ΡΠΉ ΠΎΠ±ΡΠ΅ΠΊΡ Location
/*
* Π·Π°ΠΌΠ΅ΡΡΡΠ΅, ΡΡΠΎ Π΅Π΄ΠΈΠ½ΡΡΠ²Π΅Π½Π½Π°Ρ ΡΠ°Π·Π½ΠΈΡΠ° ΠΏΡΠΈ ΡΠ°Π±ΠΎΡΠ΅ Ρ Π΄Π°Π½Π½ΠΎΠΉ Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠΎΠΉ,
* ΡΠ°ΠΊ ΠΊΠ°ΠΊ ΠΎΠ±ΡΠ΅ΠΊΡ window.location Π½Π΅Π»ΡΠ·Ρ ΠΏΠ΅ΡΠ΅Π·Π°Π³ΡΡΠ·ΠΈΡΡ, ΠΏΠΎΡΡΠΎΠΌΡ
* Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠ° history Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ ΡΡΠΎΡΠΌΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ "location" ΠΎΠ±ΡΠ΅ΠΊΡ Π²Π½ΡΡΡΠΈ
* ΠΎΠ±ΡΠ΅ΠΊΡΠ° window.history, ΠΏΠΎΡΡΠΎΠΌΡ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ Π΅Π³ΠΎ ΠΈΠ· "history.location".
* ΠΠ»Ρ Π±ΡΠ°ΡΠ·Π΅ΡΠΎΠ² ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°ΡΡΠΈΡ
"history.pushState" ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ
* ΡΡΠΎΡΠΌΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ΅ΠΊΡ "location" Ρ ΠΎΠ±ΡΡΠ½ΠΎΠ³ΠΎ "window.location".
*/
var location = window.history.location || window.location;
// Π²Π΅ΡΠ°Π΅ΠΌ ΡΠΎΠ±ΡΡΠΈΡ Π½Π° Π²ΡΠ΅ ΡΡΡΠ»ΠΊΠΈ Π² Π½Π°ΡΠ΅ΠΌ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ΅
document[eventInfo[0]](eventInfo[1] + 'click', function(event) {
event = event || window.event;
var target = event.target || event.srcElement;
// ΠΈΡΠ΅ΠΌ Π²ΡΠ΅ ΡΡΡΠ»ΠΊΠΈ Ρ ΠΊΠ»Π°ΡΡΠΎΠΌ 'ajax'
if (target && target.nodeName === 'A' &&
(' ' + target.className + ' ').indexOf('ajax') >= 0)
{
// Π·Π°Π½ΠΎΡΠΈΠΌ ΡΡΡΠ»ΠΊΡ Π² ΠΈΡΡΠΎΡΠΈΡ
history.pushState(null, null, target.href);
// ΡΡΡ ΠΌΠΎΠΆΠ΅ΡΠ΅ Π²ΡΠ·Π²Π°ΡΡ ΠΏΠΎΠ΄Π³ΡΡΠ·ΠΊΡ Π΄Π°Π½Π½ΡΡ
ΠΈ Ρ.ΠΏ.
// Π½Π΅ Π΄Π°Π΅ΠΌ Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅ ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
}
}, false);
// Π²Π΅ΡΠ°Π΅ΠΌ ΡΠΎΠ±ΡΡΠΈΠ΅ Π½Π° popstate ΠΊΠΎΡΠΎΡΠΎΠ΅ ΡΡΠ°Π±Π°ΡΡΠ²Π°Π΅Ρ ΠΏΡΠΈ Π½Π°ΠΆΠ°ΡΠΈΠΈ back/forward Π² Π±ΡΠ°ΡΠ·Π΅ΡΠ΅
window[eventInfo[0]](eventInfo[1] + 'popstate', function(event) {
// ΡΡΡ ΠΌΠΎΠΆΠ΅ΡΠ΅ Π²ΡΠ·Π²Π°ΡΡ ΠΏΠΎΠ΄Π³ΡΡΠ·ΠΊΡ Π΄Π°Π½Π½ΡΡ
ΠΈ Ρ.ΠΏ.
// ΠΏΡΠΎΡΡΠΎ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅
alert("We returned to the page with a link: " + location.href);
}, false);
})(window.addEventListener ? ['addEventListener', ''] : ['attachEvent', 'on']);
</script>
</head>
<body>
<a class="ajax" href="/mylink.html">My Link</a>
<a class="ajax" href="/otherlink.html">Other Link</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="history.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function() {
// ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ Π½ΠΎΡΠΌΠ°Π»ΡΠ½ΡΠΉ ΠΎΠ±ΡΠ΅ΠΊΡ Location
/*
* Π·Π°ΠΌΠ΅ΡΡΡΠ΅, ΡΡΠΎ Π΅Π΄ΠΈΠ½ΡΡΠ²Π΅Π½Π½Π°Ρ ΡΠ°Π·Π½ΠΈΡΠ° ΠΏΡΠΈ ΡΠ°Π±ΠΎΡΠ΅ Ρ Π΄Π°Π½Π½ΠΎΠΉ Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠΎΠΉ,
* ΡΠ°ΠΊ ΠΊΠ°ΠΊ ΠΎΠ±ΡΠ΅ΠΊΡ window.location Π½Π΅Π»ΡΠ·Ρ ΠΏΠ΅ΡΠ΅Π·Π°Π³ΡΡΠ·ΠΈΡΡ, ΠΏΠΎΡΡΠΎΠΌΡ
* Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠ° history Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ ΡΡΠΎΡΠΌΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ "location" ΠΎΠ±ΡΠ΅ΠΊΡ Π²Π½ΡΡΡΠΈ
* ΠΎΠ±ΡΠ΅ΠΊΡΠ° window.history, ΠΏΠΎΡΡΠΎΠΌΡ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ Π΅Π³ΠΎ ΠΈΠ· "history.location".
* ΠΠ»Ρ Π±ΡΠ°ΡΠ·Π΅ΡΠΎΠ² ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°ΡΡΠΈΡ
"history.pushState" ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ
* ΡΡΠΎΡΠΌΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ΅ΠΊΡ "location" Ρ ΠΎΠ±ΡΡΠ½ΠΎΠ³ΠΎ "window.location".
*/
var location = window.history.location || window.location;
// ΠΈΡΠ΅ΠΌ Π²ΡΠ΅ ΡΡΡΠ»ΠΊΠΈ ΠΈ Π²Π΅ΡΠ°Π΅ΠΌ ΡΠΎΠ±ΡΡΠΈΡ Π½Π° Π²ΡΠ΅ ΡΡΡΠ»ΠΊΠΈ Π² Π½Π°ΡΠ΅ΠΌ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ΅
$(document).on('click', 'a.ajax', function() {
// Π·Π°Π½ΠΎΡΠΈΠΌ ΡΡΡΠ»ΠΊΡ Π² ΠΈΡΡΠΎΡΠΈΡ
history.pushState(null, null, this.href);
// ΡΡΡ ΠΌΠΎΠΆΠ΅ΡΠ΅ Π²ΡΠ·Π²Π°ΡΡ ΠΏΠΎΠ΄Π³ΡΡΠ·ΠΊΡ Π΄Π°Π½Π½ΡΡ
ΠΈ Ρ.ΠΏ.
// Π½Π΅ Π΄Π°Π΅ΠΌ Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅ ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ
return false;
});
// Π²Π΅ΡΠ°Π΅ΠΌ ΡΠΎΠ±ΡΡΠΈΠ΅ Π½Π° popstate ΠΊΠΎΡΠΎΡΠΎΠ΅ ΡΡΠ°Π±Π°ΡΡΠ²Π°Π΅Ρ ΠΏΡΠΈ Π½Π°ΠΆΠ°ΡΠΈΠΈ back/forward Π² Π±ΡΠ°ΡΠ·Π΅ΡΠ΅
$(window).on('popstate', function(e) {
// ΡΡΡ ΠΌΠΎΠΆΠ΅ΡΠ΅ Π²ΡΠ·Π²Π°ΡΡ ΠΏΠΎΠ΄Π³ΡΡΠ·ΠΊΡ Π΄Π°Π½Π½ΡΡ
ΠΈ Ρ.ΠΏ.
// ΠΏΡΠΎΡΡΠΎ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅
alert("ΠΡ Π²Π΅ΡΠ½ΡΠ»ΠΈΡΡ Π½Π° ΡΡΡΠ°Π½ΠΈΡΡ ΡΠΎ ΡΡΡΠ»ΠΊΠΎΠΉ: " + location.href);
});
});
</script>
</head>
<body>
<a class="ajax" href="/mylink.html">My Link</a>
<a class="ajax" href="/otherlink.html">Other Link</a>
</body>
</html>
history.js?basepath=/pathtosite/ - Π±Π°Π·ΠΎΠ²ΡΠΉ ΠΏΡΡΡ ΠΊ ΡΠ°ΠΉΡΡ, ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ ΠΈΠΌΠ΅Π΅Ρ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΊΠΎΡΠ½Ρ "/".
history.js?redirect=true - Π²ΠΊΠ»ΡΡΠΈΡΡ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΡΡΠ»ΠΎΠΊ.
history.js?type=/ - ΠΏΠΎΠ΄ΡΡΠ°Π²Π»ΡΡΡ ΠΏΠΎΠ΄ΡΡΡΠΎΠΊΡ ΠΏΠΎΡΠ»Π΅ ΡΠΊΠΎΡΡ, ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ ΠΈΠΌΠ΅Π΅Ρ ΡΠΈΠΌΠ²ΠΎΠ» "/".
Π’Π°ΠΊΠΆΠ΅ Π²Ρ ΠΌΠΎΠΆΠ΅ΡΠ΅ ΠΊΠΎΠΌΠ±ΠΈΠ½ΠΈΡΠΎΠ²Π°ΡΡ ΠΎΠΏΡΠΈΠΈ:
history.js?type=/&redirect=true&basepath=/pathtosite/ - ΠΏΠΎΡΡΠ΄ΠΎΠΊ ΠΎΠΏΡΠΈΠΉ Π½Π΅ ΠΈΠΌΠ΅Π΅Ρ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅.
ΠΠ»ΠΈ Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΡΠΏΠ΅ΡΠΈΠ°Π»ΡΠ½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π² JavaScript:
history.redirect(/* type = */ '/', /* basepath = */ '/pathtosite/');
ΠΠ΅ΠΌΠΎ-ΡΠ°ΠΉΡ: http://history.spb-piksel.ru/ ΠΈΠ»ΠΈ http://devote.github.io/demos/history/
Π― Π² Twitter: https://twitter.com/DimaPakhtinov
FAQs
HTML5 History API expansion for browsers not supporting pushState, replaceState
The npm package html5-history-api receives a total of 781 weekly downloads. As such, html5-history-api popularity was classified as not popular.
We found that html5-history-api demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.Β It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.