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
Javascript Articles
Page 474 of 534
Fire a JavaScript function when a user finishes typing instead of on key up?
To fire a JavaScript function when the user finishes typing, try to run the following code −Example var timer = null; $("#myText").keydown(function(){ clearTimeout(timer); timer = setTimeout(doStuff, 1000) }); function doStuff() { alert("Write more!"); }
Read MoreHow to set cookies expiry date in JavaScript?
Extend the life of a cookie beyond the current browser session by setting an expiration date and saving the expiry date within the cookie. This can be done by setting the ‘expires’ attribute to a date and time.ExampleYou can try to run the following example to set the expiry date of a cookie by 1 Month − Enter name:
Read MoreWhat are the best practices for function overloading in JavaScript?
Function overloading occurs when a function performs different tasks based on a number of arguments passed to it.The best practice for function overloading with parameters is to not check the types. The code runs slower when the type is checked and it should be avoided. For this, the last parameter to the methods should be an objectAlso, do not check the argument length.ExampleHere’s an example −function display(a, b, value) { } display(30, 15, {"method":"subtract"}); display(70, 90, {"test":"equals", "val":"cost"});
Read MoreHow to write a global error handler in JavaScript?
The following global error handler will show how to catch unhandled exception −Example window.onerror = function(errMsg, url, line, column, error) { var result = !column ? '' : 'column: ' + column; result += !error; document.write("Error= " + errMsg + "url= " + url + "line= " + line + result); var suppressErrorAlert = true; return suppressErrorAlert; }; setTimeout(function() { eval("{"); }, 500)
Read MoreWhy are parenthesis used to wrap a JavaScript function call?
In JavaScript, the functions wrapped with parenthesis are called “Immediately Invoked Function Expressions" or "Self Executing Functions.The purpose of wrapping is to namespace and control the visibility of member functions. It wraps code inside a function scope and decrease clashing with other libraries. This is what we call Immediately Invoked Function Expression (IIFE) or Self Executing Anonymous Function.Here’s the syntax −(function() { // code })();As you can see above, the following pair of parentheses converts the code inside the parentheses into an expression −function(){...}In addition, the next pair, i.e. the second pair of parentheses continues the operation. It calls ...
Read MoreWhat is the difference between decodeURIComponent and decodeURI?
decodeURIComponentTo decode a URL component in JavaScript, use the decodeURLComponent() method.ExampleYou can try to run the following code to decode a URL component − Check function display() { var uri = "http://example.com/welcome msg.jsp?name=åmit&sub=programming"; // first encode var encode = encodeURIComponent(uri); var decode = decodeURIComponent(encode); var result = "Encode= " + ...
Read MoreWhat is the difference between single and double quotes in JavaScript?
In JavaScript, use any of the single or double quotes for a string. However, you should be consistent in whatever you select. Single and double quotes are the same in JavaScript −"Let us say: \"Life's good!\"" 'Let us say: "Life\'s good!"' “Let us say: \"Life\'s good!\"" 'Let us say: \"Life\'s good!\"'Let’s see an example, which is supported by ES6 −`Will you be "my" friend. I really liked it when I was a kid,.`;
Read MoreHow do I split a string, breaking at a particular character in JavaScript?
To split a string on every occurrence of ~, split the array. After splitting, add a line break i.e. for each occurrence of ~. For example, This is demo text 1!~This is demo text 2!~~This is demo text 3! After the split and adding line breaks like the following for ~ occurrence − This is demo text 1! This is demo text 2! This is demo text 3! Example Let's see the complete example Adding line break var myArray = 'This is demo text 1!~This is demo text 2!~ ~This is demo text 3!~This is ...
Read MoreWhat is the (function() { } )() construct in JavaScript?
The (function() { } )() construct is an immediately invoked function expression (IIFE). It is a function, which executes on creation.SyntaxHere’s the syntax −(function() { // code })();As you can see above, the following pair of parentheses converts the code inside the parentheses into an expression −function(){...}In addition, the next pair, i.e. the second pair of parentheses continues the operation. It calls the function, which resulted from the expression above.
Read MoreHow to fix problems related to the JavaScript Void 0 Error?
JavaScript void is an error, which can be seen on the web browser. This happened when a user blocks JavaScript coding on the web browser. This gives the void error when an attempt is made to run.The fix is to enable JavaScript. Let us see how to enable it in Firefox web browser −Open Firefox web browser and click Options. After clicking, you will reach the Settings. Here, click the Content tab as shown below −Now, check the box to enable JavaScript. After enabling, click the “Ok” button to save the settings.In newer versions of Firefox, you will get JavaScript ...
Read More