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
Front End Technology Articles
Page 332 of 652
How to use the 'with' keyword in JavaScript?
The with keyword in JavaScript creates a shorthand for referencing an object's properties and methods. However, it's deprecated and not recommended in modern JavaScript due to performance and security issues. Syntax with (object) { // properties used without the object name and dot } Basic Example Here's how with works with a simple object: JavaScript with Statement var person = { ...
Read MoreHow to list down all the cookies by name using JavaScript?
To list all cookies by name in JavaScript, you access the document.cookie property, split it into individual cookie pairs, and extract the name-value pairs from each cookie. How document.cookie Works The document.cookie property returns all cookies as a single string, with each cookie separated by semicolons. For example: "name1=value1; name2=value2; name3=value3". Method 1: Basic Cookie Listing Here's a simple approach to list all cookies by splitting the cookie string: function ReadCookie() { ...
Read MoreHow to use JavaScript to set cookies for homepage only?
Setting cookies for a specific page like the homepage requires checking the current page URL before creating the cookie. This ensures the cookie is only set when users are on the designated homepage. Understanding the Approach To restrict cookie setting to the homepage only, we need to: Get the current page URL using window.location.pathname Check if the current page matches our homepage criteria Set the cookie only if the condition is met Example: Setting Cookies for Homepage Only ...
Read MoreHow to use JavaScript to set cookies for a specific page only?
We can set cookies for a specific page only using JavaScript. We use the path attribute of the document.cookie property to set the cookie on a specific webpage. Cookies are small text files (4 KB) that store important information such as username, email, session id, and other preferences that help customize the webpage for a particular user. The pathname property of the Window location returns a string containing the path of the current webpage. The path is basic information about where the current webpage is stored on the server. document.cookie Syntax The document.cookie property returns a list ...
Read MoreHow to create domain-based cookies using JavaScript?
To create a domain-based cookie, set the domain and path attribute on your cookie. Domain-based cookies are accessible across all subdomains of the specified domain. Syntax document.cookie = "cookieName=value; domain=.example.com; path=/"; Domain Cookie Format domain=.example.com The dot prefix (.) makes the cookie available to all subdomains like www.example.com, api.example.com, etc. Example: Setting Domain-Based Cookie function WriteCookie() { if( document.myform.customer.value == "" ...
Read MoreHow can I list all cookies on the current page with JavaScript?
In JavaScript, you can list all cookies on the current page using the document.cookie property. This property returns a string containing all cookies as semicolon-separated key-value pairs. Basic Cookie Retrieval The simplest way to get all cookies is to access document.cookie directly: // Set some sample cookies first document.cookie = "username=john; path=/"; document.cookie = "theme=dark; path=/"; ...
Read MoreHow to store large data in JavaScript cookies?
JavaScript cookies have a size limit of approximately 4KB per cookie, making them unsuitable for storing large amounts of data directly. Here are several effective strategies to handle large data storage needs. Cookie Size Limitations Before exploring solutions, it's important to understand that: Each cookie is limited to ~4KB (4096 bytes) Browsers typically allow 20-50 cookies per domain Total storage per domain is usually limited to 4KB × number of cookies Method 1: Using Session IDs with Server Storage Store large data on the server and use a session ID in the cookie ...
Read MoreWhat is a NaN property of a Number object in JavaScript?
In JavaScript, the NaN property is a special value that represents "Not a Number". It is a property of the Number object and can be accessed using Number.NaN. The NaN property is usually produced as a result of an operation that cannot produce a meaningful result. For example, dividing 0 by 0 or trying to parse an invalid number will both produce NaN. Common Operations That Produce NaN console.log(Math.sqrt(-1)); // NaN console.log(0/0); // NaN console.log(parseInt("foo")); // NaN console.log("hello" * 2); ...
Read MoreHow to get a number value of a string in Javascript?
To extract numeric values from strings in JavaScript, you have several approaches depending on your needs. The most common methods are using regular expressions with parseInt(), parseFloat(), or Number(). Using parseInt() with Regular Expression For extracting integers from strings containing mixed content, combine parseInt() with match() and a regex pattern: var myStr = "abcdef 30"; var num = parseInt(myStr.match(/\d+/)); console.log("Extracted number:", num); ...
Read MoreWhat is the shortest function of reading a cookie by name in JavaScript?
The shortest way to read a cookie by name in JavaScript is using a one-line function that searches through document.cookie and extracts the specific value. Shortest Cookie Reader Function Here's the most concise function to read a cookie by name: Cookie Reader // Set some cookies first for testing document.cookie = "username=john; path=/"; document.cookie = "theme=dark; path=/"; ...
Read More