Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
The Network Information API tells you about the user's connection type and speed. You can optimize content delivery based on their network!
Check your current connection details
<div style="max-width: 600px; margin: 0 auto; font-family: sans-serif;">
<h2 style="color: #4f46e5; margin-bottom: 20px;">📶 Network Info Demo</h2>
<div style="background: #e0e7ff; padding: 20px; border-radius: 8px; margin-bottom: 20px; border: 3px solid #6366f1;">
<button id="checkBtn" style="padding: 12px 24px; background: #4f46e5; color: white; border: none; border-radius: 6px; cursor: pointer; font-weight: 600; font-size: 16px; width: 100%; margin-bottom: 15px;">
📶 Check Network Info
</button>
<div id="info" style="padding: 15px; background: white; border-radius: 6px; display: none;"></div>
</div>
<div style="background: #dbeafe; padding: 15px; border-radius: 6px; border-left: 4px solid #3b82f6;">
<strong style="color: #1e40af;">💡 Tip:</strong>
<span style="color: #1e3a8a;"> Try switching between WiFi and mobile data to see changes!</span>
</div>
</div>Loading preview...
// Get connection information
const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
if (connection) {
// Connection type (e.g., 'wifi', 'cellular', '4g')
console.log('Type:', connection.effectiveType);
// Downlink speed in Mbps
console.log('Speed:', connection.downlink + ' Mbps');
// Round-trip time in milliseconds
console.log('RTT:', connection.rtt + ' ms');
// Data saver mode enabled?
console.log('Save Data:', connection.saveData);
} else {
console.log('Network Information API not supported');
}
// effectiveType values:
// - 'slow-2g' - Very slow
// - '2g' - Slow
// - '3g' - Medium
// - '4g' - Fast
// 🎯 Use to optimize content delivery// Load images based on connection speed
function loadImage() {
const connection = navigator.connection;
const img = document.getElementById('hero-image');
if (!connection) {
// No API support - load normal image
img.src = 'hero-normal.jpg';
return;
}
// Check connection type
if (connection.saveData) {
// User enabled data saver
img.src = 'hero-low.jpg';
console.log('Loading low-quality image (data saver on)');
} else if (connection.effectiveType === '4g') {
// Fast connection - load high quality
img.src = 'hero-high.jpg';
console.log('Loading high-quality image');
} else if (connection.effectiveType === '3g') {
// Medium connection - load normal
img.src = 'hero-normal.jpg';
console.log('Loading normal-quality image');
} else {
// Slow connection - load low quality
img.src = 'hero-low.jpg';
console.log('Loading low-quality image (slow connection)');
}
}
loadImage();
// Listen for connection changes
navigator.connection?.addEventListener('change', () => {
console.log('Connection changed:', navigator.connection.effectiveType);
loadImage(); // Reload with appropriate quality
});
// 🎯 Better UX on slow connections!Serve lower quality on slow connections
Auto-adjust video quality (like Netflix)
Respect user's data saving preference
Only preload resources on fast connections
const conn = navigator.connection;
console.log(conn.effectiveType); // '4g', '3g', etc.
console.log(conn.downlink); // Mbps
console.log(conn.saveData); // true/falsenavigator.connection?.addEventListener('change', () => {
console.log('Connection changed');
});💾 Data Saver: Respect saveData preference
Connection speed
slow-2g, 2g, 3g, 4g
Data saver mode
Respect user choice
Download speed
In Mbps
Optimize for network
Better performance