Skip to content

Instantly share code, notes, and snippets.

@luislobo14rap
Last active May 12, 2023 20:13
Show Gist options
  • Save luislobo14rap/1082bda3f3009bf1698058ae50477459 to your computer and use it in GitHub Desktop.
Save luislobo14rap/1082bda3f3009bf1698058ae50477459 to your computer and use it in GitHub Desktop.
change-url-without-reload.js
// change-url-without-reload.js v1
function changeURLWithoutReload(newUrl){
let url = getURL();
return window.history.pushState(undefined, undefined, url + '/' + newUrl.replace(/^\//, ''));
};
// getURL.js v1
function getURL(url = window.location.href){
return url.split(/[?#&]/)[0].replace(/\/+$/, '');
};
let url = window.location.href
if( (/\d{5,}/).test(url) ) {
window.history.pushState({}, '', url.replace(/\d{5,}/, 'newlink') );
} else {
window.history.pushState({}, '', url + 'pin/newlink/' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment