A standalone, easy-to-distribute font switcher widget that allows users to switch between Original, Ranjana (Nithya Ranjana), and Noto Sans Newa fonts on any webpage.
<link rel="stylesheet" href="font-switcher.css">
<script src="font-switcher.js"></script>
<script>
FontSwitcher.init();
</script>
You can customize the widget behavior by passing options to FontSwitcher.init():
<script>
FontSwitcher.init({
targetSelector: 'body', // CSS selector for elements to apply fonts (default: 'body')
targetClasses: null, // Array of class names (e.g., ['content', 'article']) - takes precedence over targetSelector
container: null, // CSS selector or DOM element to insert widget into (null = fixed top-right)
backgroundColor: null, // Custom background color (e.g., '#2c3e50', 'transparent', 'rgba(0,0,0,0.1)')
position: 'fixed', // Widget position (default: 'fixed')
autoLoad: true // Auto-load Google Fonts (default: true)
});
</script>
<script>
FontSwitcher.init({ targetSelector: 'body' });
</script>
<script>
FontSwitcher.init({ targetSelector: '.content' });
</script>
<script>
FontSwitcher.init({ targetSelector: 'article, .main-content' });
</script>
<script>
FontSwitcher.init({ targetClasses: ['content', 'article', 'main-text'] });
</script>
<script>
FontSwitcher.init({ targetClasses: ['readable-content'] });
</script>
Note: When targetClasses is specified, it takes precedence over targetSelector. The class names should be provided without the leading dot (e.g., 'content' not '.content').
<!-- HTML -->
<nav id="main-nav">
<!-- Your navigation items -->
</nav>
<!-- JavaScript -->
<script>
// Using CSS selector
FontSwitcher.init({
container: '#main-nav',
targetClasses: ['content']
});
// Or using DOM element
const navElement = document.getElementById('main-nav');
FontSwitcher.init({
container: navElement,
targetClasses: ['content']
});
</script>
<script>
// No container specified = fixed top-right (default behavior)
FontSwitcher.init({
targetClasses: ['content']
});
</script>
<script>
// Dark background (text color auto-adjusts for contrast)
FontSwitcher.init({
backgroundColor: '#2c3e50',
targetClasses: ['content']
});
// Transparent background
FontSwitcher.init({
backgroundColor: 'transparent',
targetClasses: ['content']
});
// Semi-transparent background
FontSwitcher.init({
backgroundColor: 'rgba(0, 0, 0, 0.1)',
targetClasses: ['content']
});
// Match navigation bar color
FontSwitcher.init({
container: '#main-nav',
backgroundColor: '#2c3e50', // Match nav background
targetClasses: ['content']
});
</script>
Note: When a dark background color is detected, the widget automatically adjusts text colors to white for better contrast and readability.
font-switcher.css - Widget styles and font definitionsfont-switcher.js - Core widget functionalityWorks in all modern browsers that support:
Same license as the Nithya Ranjana font project.