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 380 of 534
Resize image before submitting the form HTML5
To resize an image before submitting a form in HTML5, you can use the HTML5 Canvas API with the drawImage() method. This allows you to scale images on the client-side before sending them to the server. Basic Approach The process involves loading the image into a canvas, scaling it using drawImage(), and then converting the canvas back to an image format for form submission. Syntax context.drawImage(img, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight); Parameters img - The image element to draw sx, sy - Source x and y coordinates (usually ...
Read MoreCount the number of data types in an array - JavaScript
We are required to write a JavaScript function that takes in an array that contains elements of different data types and the function should return a map representing the frequency of each data type. Let's say the following is our array: const arr = [23, 'df', undefined, null, 12, { name: 'Rajesh' }, [2, 4, 7], 'dfd', null, Symbol('*'), 8]; Understanding Data Types in JavaScript JavaScript's typeof operator returns string representations of data types. Note that arrays and null both return "object", which is a known quirk of JavaScript. Example ...
Read MoreDifference between dragenter and dragover event in HTML5
HTML5 drag and drop API provides several events to handle dragging operations. Two commonly confused events are dragenter and dragover. While both are essential for drag and drop functionality, they serve different purposes. dragenter Event The dragenter event fires when a dragged element enters a valid drop target. This event is used to determine whether the drop target will accept the drop operation. To accept the drop, you must prevent the default behavior by calling preventDefault(). Drag me Drop zone ...
Read MoreWhy HTML5 Web Workers are useful?
JavaScript was designed to run in a single-threaded environment, meaning multiple scripts cannot run at the same time. Consider a situation where you need to handle UI events, query and process large amounts of API data, and manipulate the DOM. JavaScript will hang your browser in situations where CPU utilization is high. Let us take a simple example where JavaScript goes through a big loop: Big for loop function bigLoop(){ for (var i = 0; i
Read MoreLog error to console with Web Workers in HTML5
Web Workers in HTML5 provide a way to run JavaScript in the background without blocking the main thread. When working with Web Workers, proper error handling is crucial for debugging and maintaining application stability. Error Handling with worker.onerror The worker.onerror event handler catches errors that occur within the Web Worker and allows you to log them to the console for debugging purposes. Example Here's a complete example showing how to implement error handling in Web Workers: Web Worker Error ...
Read MoreBeginning and end pairs in array - JavaScript
We are required to write a JavaScript function that takes in an array of Number / String literals and returns another array of arrays. With each subarray containing exactly two elements, the nth element from start and nth from last. For example, if we have an array: const arr = [1, 2, 3, 4, 5, 6]; Then the output should be: const output = [[1, 6], [2, 5], [3, 4]]; How It Works The algorithm pairs elements from both ends of the array moving inward: ...
Read MoreHow to check web browser support in HTML5
Checking web browser support for HTML5 features is essential for creating compatible web applications. This guide shows you how to detect HTML5 features using both modern JavaScript methods and the Modernizr library. Using Modernizr Library Modernizr is a popular JavaScript library that detects HTML5 and CSS3 features. Here's how to check for web worker support: Web Worker Support Check function checkWebWorkerSupport(){ ...
Read MoreRandom name generator function in JavaScript
We are required to write a JavaScript function that takes in a number n and returns a random string of length n containing no other than the 26 English lowercase alphabets. Example Let us write the code for this function: const num = 8; const randomNameGenerator = num => { let res = ''; for(let i = 0; i < num; i++){ const random = Math.floor(Math.random() * 26); res += String.fromCharCode(97 + ...
Read MoreHow to detect a particular feature through JavaScript with HTML
Feature detection in JavaScript allows you to check if a browser supports specific HTML5 features before using them. This ensures your web application works across different browsers and devices. Using Native JavaScript Feature Detection You can detect features using native JavaScript without external libraries: // Detect audio support function supportsAudio() { return !!document.createElement('audio').canPlayType; } // Detect video support function supportsVideo() { return !!document.createElement('video').canPlayType; } // Detect canvas support function supportsCanvas() { var canvas = document.createElement('canvas'); return !!(canvas.getContext && ...
Read MoreNearest Prime to a number - JavaScript
We are required to write a JavaScript function that takes in a number and returns the first prime number that appears after n. For example: If the number is 24, then the output should be 29. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, etc. Implementation We'll create two functions: one to check if a number is prime, and another to find the nearest prime after a given ...
Read More