EzDevInfo.com

javascript

JavaScript Style Guide

How can I get query string values in JavaScript?

Is there a plugin-less way of retrieving query string values via jQuery (or without)?

If so, how? If not, is there a plugin which can do so?


Source: (StackOverflow)

For-each over an array in JavaScript?

How can I loop through all the objects in an array using JavaScript?

I thought of something like this (where objects is my array of objects):

forEach(instance in objects)

But this does not seem to be correct.


Source: (StackOverflow)

Advertisements

What is the difference between call and apply?

What is the difference between using call and apply to invoke a function?

var func = function(){
  alert('hello!');
};

func.apply();

vs

func.call();

Are there performance differences between the two methods? When is it best to use call over apply and vice versa?


Source: (StackOverflow)

Validate email address in JavaScript?

How can an email address be validated in JavaScript?


Source: (StackOverflow)

Capitalize the first letter of string in JavaScript

How do I capitalize the first character of a string, but not change the case of any of the other letters?

For example:

  • this is a test -> This is a test
  • the Eiffel Tower -> The Eiffel Tower
  • /index.html -> /index.html

Source: (StackOverflow)

How do JavaScript closures work?

Like the old Albert Einstein said:

If you can't explain it to a six-year-old, you really don't understand it yourself.

Well, I tried to explain JavaScript closures to a 27-year-old friend and completely failed.

How would you explain it to someone with a knowledge of the concepts which make up closures (for example, functions, variables and the like), but does not understand closures themselves?

I have seen the Scheme example given on Stack Overflow, and it did not help.


Source: (StackOverflow)

How can I check if one string contains another substring?

How can I check if one string contains another substring in JavaScript? Usually I would expect a String.contains() method, but there doesn't seem to be one.


Source: (StackOverflow)

Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?

I'm using JSLint to go through JavaScript, and it's returning many suggestions to replace == (two equals signs) with === (three equals signs) when doing things like comparing idSele_UNVEHtype.value.length == 0 inside of an if statement.

Is there a performance benefit to replacing == with ===?

Any performance improvement would be welcomed as many comparison operators exist.

If no type conversion takes place, would there be a performance gain over ==?


Source: (StackOverflow)

How do I copy to the clipboard in JavaScript?

What is the best way to copy text to the clipboard? (multi-browser)

I have tried:

function copyToClipboard(text) {
    if (window.clipboardData) { // Internet Explorer
        window.clipboardData.setData("Text", text);
    } else {  
        unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
        const clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);  
        clipboardHelper.copyString(text);
    }
}

but in Internet Explorer it gives a syntax error. In Firefox, it says unsafeWindow is not defined.

Edit A nice trick without flash: How does Trello access the user's clipboard?


Source: (StackOverflow)

array.contains(obj) in JavaScript

What is the most concise and efficient way to find out if a JavaScript array contains an obj?

This is the only way I know to do it:

function contains(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

Is there a better and more concise way to accomplish this?

This is very closely related to Stack Overflow question Best way to find an item in a JavaScript Array? which addresses finding objects in an array using indexOf.


Source: (StackOverflow)

Href attribute for JavaScript links: "#" or "javascript:void(0)"?

The following are two methods of building a link that has the sole purpose of running JavaScript code. Which is better, in terms of functionality, page load speed, validation purposes, etc.?

function myJsFunc() {
  alert("myJsFunc");
}
<a rel='nofollow' href="#" onclick="myJsFunc();">Run JavaScript Code</a>

or

  function myJsFunc() {
    alert("myJsFunc");
  }
 <a rel='nofollow' href="javascript:void(0)" onclick="myJsFunc();">Run JavaScript Code</a>


Source: (StackOverflow)

var functionName = function() {} vs function functionName() {}

I've recently started maintaining someone else's JavaScript code. I'm fixing bugs, adding features and also trying to tidy up the code and make it more consistent.

The previous developer uses two ways of declaring functions and I can't work out if there is a reason behind it or not.

The two ways are:

var functionOne = function() {
    // Some code
};
function functionTwo() {
    // Some code
}

What are the reasons for using these two different methods and what are the pros and cons of each? Is there anything that can be done with one method that can't be done with the other?


Source: (StackOverflow)

Validate decimal numbers in JavaScript - IsNumeric()

What's the cleanest, most effective way to validate decimal numbers in JavaScript?

Bonus points for:

  1. Clarity. Solution should be clean and simple.
  2. Cross-platform.

Test cases:

 01. IsNumeric('-1') => true
 02. IsNumeric('-1.5') => true
 03. IsNumeric('0') => true
 04. IsNumeric('0.42') => true
 05. IsNumeric('.42') => true
 06. IsNumeric('99,999') => false
 07. IsNumeric('0x89f') => false
 08. IsNumeric('#abcdef')=> false
 09. IsNumeric('1.2.3') => false
 10. IsNumeric('') => false
 11. IsNumeric('blah') => false

Source: (StackOverflow)

Remove a specific element from an array in JavaScript?

I have an array of integers, which I'm using the .push() method to add to.

Is there a simple way to remove a specific element from an array? The equivalent of something like array.remove(int);

I have to use core JavaScript - no frameworks are allowed.


Source: (StackOverflow)

Include a JavaScript file in another JavaScript file?

Is there something similar to @import in CSS in JavaScript that allows you to include a JavaScript file inside another JavaScript file?


Source: (StackOverflow)