Skip to content

Instantly share code, notes, and snippets.

@kdzwinel
Last active February 13, 2020 11:18
Show Gist options
  • Save kdzwinel/a15545974e60f349c9ad to your computer and use it in GitHub Desktop.
Save kdzwinel/a15545974e60f349c9ad to your computer and use it in GitHub Desktop.
Reload CSS files without reloading the page
function reloadCSS() {
const links = document.getElementsByTagName('link');
Array.from(links)
.filter(link => link.rel.toLowerCase() === 'stylesheet' && link.href)
.forEach(link => {
const url = new URL(link.href, location.href);
url.searchParams.set('forceReload', Date.now());
link.href = url.href;
});
}
@kdzwinel
Copy link
Author

reloadcss mov

@stowball
Copy link

You could use querySelectorAll('link[rel="stylesheet"]') to remove the need to filter()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment