Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 367 of 534
HTML5 video full preload in JavaScript
The oncanplaythrough event fires when the browser estimates it can play the video through to the end without stopping for buffering. This is useful for ensuring full video preload before playback. Understanding canplaythrough Event The canplaythrough event indicates the browser has buffered enough data to play the entire video without interruption. This differs from canplay, which only ensures playback can start. Example: Basic Video Preload Detection Video Preload Example ...
Read MoreHow to make the user login for an offline web app?
Offline web apps require a different authentication approach since they can't always connect to servers. The key is implementing local authentication with data synchronization when online. How Offline Login Works When online, authenticate against the server and store user credentials locally. When offline, authenticate against the local storage instead of the remote server. User Login Online? Check Server Offline? Check Local Grant Access ...
Read MoreHow to make an Ember.js app offline with server sync when available?
Making an Ember.js app work offline with server synchronization requires implementing client-side storage that can sync data when connectivity is restored. This enables users to continue working with your app even without internet access. Using LocalStorage Adapter The ember-localstorage adapter provides basic offline storage capabilities by storing data in the browser's localStorage: App.store = DS.Store.create({ revision: 11, adapter: DS.LSAdapter.create() }); Advanced Offline Storage with IndexedDB For more robust offline functionality, use IndexedDB which provides better performance and storage capacity: App.Store = DS.SyncStore.extend({ ...
Read MoreRecursive multiplication in array - JavaScript
We need to write a JavaScript function that takes a nested array containing numbers, false values, zeros, and strings, then returns the product of all valid numbers. The function should ignore zeros and falsy values while recursively processing nested arrays. Problem Statement Given a nested array with mixed data types, we want to multiply only the truthy numeric values while ignoring zeros, null, undefined, false, and strings. Solution We'll use recursion to traverse nested arrays and multiply only valid numbers: const arr = [1, 5, 2, null, [ 2, 5, ...
Read MoreHTML5 Canvas Font Size Based on Canvas Size
When working with HTML5 Canvas, you often need to scale font sizes dynamically based on the canvas dimensions to maintain proportional text across different screen sizes. The Problem Fixed font sizes don't adapt when canvas dimensions change, making text too small on large canvases or too large on small ones. Solution: Proportional Font Scaling Use a ratio-based approach to calculate font size relative to canvas width: var fontBase = 800; // Base canvas width var fontSize = 60; // Desired font size at base width function getFont(canvas) { ...
Read MoreFind Second most frequent character in array - JavaScript
We are required to write a JavaScript function that takes in an array and returns the element that appears for the second most number of times. Let's say the following is our array: const arr = [1, 34, 4, 3, 2, 1, 4, 6, 4, 6, 5, 3, 6, 6]; The most frequent element is 6 (appears 4 times), but we want the second most frequent element, which is 4 (appears 3 times). Example const arr = [1, 34, 4, 3, 2, 1, 4, 6, 4, 6, 5, 3, 6, 6]; ...
Read MoreProper use of flex properties when nesting flex containers
A flex container is always the parent and a flex item is always the child. The scope of a flex formatting context is limited to a parent/child relationship. Descendants of a flex container beyond the children are not part of flex layout and will not accept flex properties. There are certain flex properties that apply only to flex containers: justify-content flex-wrap flex-direction align-items align-content There are certain flex properties that apply only to flex items: ...
Read MoreDivide a string into n equal parts - JavaScript
We are required to write a JavaScript function that takes in a string and a number n (such that n exactly divides the length of string). We need to return an array of string of length n containing n equal parts of the string. Let's write the code for this function − Example const str = 'this is a sample string'; const num = 5; const divideEqual = (str, num) => { const len = str.length / num; const creds = str.split("").reduce((acc, val) => { ...
Read MoreHow to stop dragend's default behavior in drag and drop?
To stop dragend's default behavior in drag and drop operations, you need to use preventDefault() and detect if the mouse is over a valid drop target. Here are several approaches to handle this properly. Using preventDefault() Method The most straightforward way is to call preventDefault() in the dragend event handler: Drag me Drop here const dragItem = document.getElementById('dragItem'); const dropZone = document.getElementById('dropZone'); dragItem.addEventListener('dragend', function(event) { event.preventDefault(); console.log('Dragend default behavior prevented'); }); dragItem.addEventListener('dragstart', function(event) { event.dataTransfer.setData('text/plain', event.target.id); }); dropZone.addEventListener('dragover', function(event) ...
Read MoreHTML5 Canvas Degree Symbol
The HTML5 Canvas API allows you to display special characters like the degree symbol (°) in text. You can use HTML entities or Unicode characters to render the degree symbol on canvas. Using HTML Entity for Degree Symbol The most common approach is using the HTML entity ° which renders as the degree symbol (°). body { ...
Read More