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
Programming Scripts Articles
Page 7 of 33
What is the usage of onunload event in JavaScript?
The onunload event triggers when a user navigates away from a page, closes the browser tab, or refreshes the page. This event is useful for cleanup tasks like saving user data or showing farewell messages. Syntax // HTML attribute // JavaScript event listener window.addEventListener('unload', function(event) { // Cleanup code }); Example: Using onunload with HTML Attribute Close the page and see what happens! This event may give unexpected results ...
Read MoreHow to return a string representing the source for an equivalent Date object?
The toSource() method was a non-standard JavaScript method that returned a string representing the source code of a Date object. However, this method has been deprecated and removed from modern browsers. The Deprecated toSource() Method The toSource() method was originally available in Firefox but was never part of the ECMAScript standard. It would return a string that could theoretically recreate the Date object: // This method is deprecated and no longer works var dt = new Date(2018, 0, 15, 14, 39, 7); // dt.toSource() would have returned: (new Date(1516022347000)) Modern Alternatives Since toSource() ...
Read MoreWhat is nodeValue property in JavaScript HTML DOM?
The nodeValue property in JavaScript HTML DOM returns the value of a node. It returns null for element nodes and the actual content for text nodes. Syntax node.nodeValue Return Value The nodeValue property returns: Text nodes: The text content Comment nodes: The comment text Element nodes: null Attribute nodes: The attribute value Example: Getting Text Node Value You can try to run the following code to learn how to get nodeValue property. ...
Read MoreHow to apply a function simultaneously against two values of the array from left-to-right?
The reduce() current) ? prev : current; }); document.write("Maximum value: " + max + ""); // Count occurrences var fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']; var count = fruits.reduce(function(acc, fruit) { ...
Read MoreHow to add properties and methods to an object in JavaScript?
In JavaScript, you can add properties and methods to objects using several approaches. The most common methods include direct assignment, using the prototype property for constructor functions, and using Object.defineProperty(). Method 1: Direct Property Assignment The simplest way to add properties to an existing object is direct assignment: Direct Property Assignment let car = { brand: "Toyota", ...
Read MoreHow to convert Boolean to Number in JavaScript?
In JavaScript, Boolean values (true and false) can be converted to numbers (1 and 0 respectively) using several methods. This conversion is useful when performing mathematical operations or comparisons that require numeric values. JavaScript provides multiple approaches to convert Boolean values to numbers. Let's explore three effective methods with examples. Using the Number() Function The Number() function is the most explicit and readable way to convert Boolean values to numbers. It converts true to 1 and false to 0. Syntax let result = Number(booleanValue); Example ...
Read MoreWhich event occurs in JavaScript when an element is content is copied to clipboard?
When a user copies an element's content, the oncopy event triggers in JavaScript. This event fires when the user performs a copy operation using Ctrl+C, right-click copy, or any other method to copy selected content. Syntax element.oncopy = function() { // Code to execute when content is copied }; // Or using addEventListener element.addEventListener('copy', function() { // Code to execute when content is copied }); Example: Basic oncopy Event ...
Read MoreHow to display the title of a document with JavaScript?
In JavaScript, you can display a document's title using two main approaches: the document.title property and the document.getElementsByTagName() method. This tutorial demonstrates both methods with practical examples. Using document.title Property The document.title property is the most straightforward way to access and display an HTML document's title. It directly returns the title as a string. Syntax document.title Example 1 This example shows how to display the document title by clicking a button: This is the title accessed from the document ...
Read MoreWhat is the role of altKey Mouse Event in JavaScript?
The altKey property of mouse events in JavaScript indicates whether the Alt key was pressed when a mouse button was clicked. This property returns a boolean value: true if the Alt key was held down, false otherwise. Syntax event.altKey Return Value Returns true if the Alt key was pressed during the mouse event, false if it was not pressed. Example The following example demonstrates how to detect whether the Alt key was pressed during a mouse click: Press and hold ALT key and ...
Read MoreHow to force a number to display in exponential notation?
Use the toExponential() method to force a number to display in exponential notation, even if the number is in the range in which JavaScript normally uses standard notation. Syntax number.toExponential(fractionDigits) Parameters fractionDigits - An optional integer specifying the number of digits after the decimal point. Defaults to as many digits as necessary to specify the number. Return Value Returns a string representing the number in exponential notation. Example: Basic Usage You can force a number to display in exponential notation as follows: ...
Read More