smallseo.info

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)

How do you get a timestamp in JavaScript?

How can I get a timestamp in JavaScript?

Something similar to Unix's timestamp, that is, a single number that represents the current time and date. Either as a number or a string.


Source: (StackOverflow)

Create GUID / UUID in JavaScript?

I'm trying to create globally-unique identifiers in JavaScript. I'm not sure what routines are available on all browsers, how "random" and seeded the built-in random number generator is, etc..

The GUID / UUID should be at least 32 characters and should stay in the ASCII range to avoid trouble when passing them around.


Source: (StackOverflow)

Detecting an undefined object property

What's the best way of checking if an object property in JavaScript is undefined?


Source: (StackOverflow)

What is the most efficient way to clone an object?

What is the most efficient way to clone a JavaScript object? I've seen obj = eval(uneval(o)); being used, but that's currently Firefox-only. In Mootools 1.2, I've done things like obj = JSON.decode(JSON.encode(o)); but question the efficiency.

I've also seen recursive copying functions with various flaws. I'm surprised no canonical solution exists.


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)

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)

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)

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 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)

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)