Javascript Articles

Page 474 of 534

Fire a JavaScript function when a user finishes typing instead of on key up?

Krantik Chavan
Krantik Chavan
Updated on 16-Jun-2020 367 Views

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 More

How to set cookies expiry date in JavaScript?

radhakrishna
radhakrishna
Updated on 16-Jun-2020 6K+ Views

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 More

What are the best practices for function overloading in JavaScript?

Nitya Raut
Nitya Raut
Updated on 16-Jun-2020 313 Views

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 More

How to write a global error handler in JavaScript?

mkotla
mkotla
Updated on 16-Jun-2020 2K+ Views

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 More

Why are parenthesis used to wrap a JavaScript function call?

Rishi Rathor
Rishi Rathor
Updated on 16-Jun-2020 5K+ Views

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 More

What is the difference between decodeURIComponent and decodeURI?

Vikyath Ram
Vikyath Ram
Updated on 16-Jun-2020 497 Views

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 More

What is the difference between single and double quotes in JavaScript?

Kumar Varma
Kumar Varma
Updated on 16-Jun-2020 260 Views

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 More

How do I split a string, breaking at a particular character in JavaScript?

Anvi Jain
Anvi Jain
Updated on 16-Jun-2020 258 Views

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 More

What is the (function() { } )() construct in JavaScript?

Rishi Rathor
Rishi Rathor
Updated on 16-Jun-2020 2K+ Views

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 More

How to fix problems related to the JavaScript Void 0 Error?

Arjun Thakur
Arjun Thakur
Updated on 16-Jun-2020 2K+ Views

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
Showing 4731–4740 of 5,339 articles
« Prev 1 472 473 474 475 476 534 Next »
Advertisements