Dark Launch

This is a Dark Launch.

JavaScript isArray type to distinguish between array or object

The typeof operator will say that an array is an "object". To accurately identify an array from an object in JavaScript, use the following solution:

Javascript
function isArray(var) {
    return Object.prototype.toString.apply(var) === "[object Array]";
}

Instead of using typeof or instanceof, use the Object.prototype.toString.apply method to check if a variable is an object or is an array in JavaScript.

Javascript
Object.prototype.toString.apply( $("body") )
// "[object Object]"
 
Object.prototype.toString.apply( [{foo:"bar"}] )
// "[object Array]"

As an alternate solution, use the toString.call(element) method.

Javascript
toString.call( $("body") )
// "[object Object]"
 
toString.call( [{foo:"bar"}] )
// "[object Array]"

jQuery type, isArray and isPlainObject functions are also available.

Javascript
$.type(element)
$.isArray(element)
$.isPlainObject(element)
Javascript
$.type( [{foo:"bar"}] )
// "array"
 
$.type( $("body") )
// "object"
Javascript
$.isArray( [{foo:"bar"}] )
// true
 
$.isArray( $("body") )
// false

JavaScript for loop using setTimeout to pass argument

When using a for loop with setTimeout, you may encounter unexpected behavior.

Consider the following code. What do you expect the console to display?

Javascript
var time = 0;
for (var i = 0; i < 5; i++) {
    time += 1000;
    setTimeout(function() {
        console.log("var is now", i);
    }, time);
}

You may have expected this:

Code
var is now 1
var is now 2
var is now 3
var is now 4
var is now 5

The code actually results in:

Code
var is now 5
var is now 5
var is now 5
var is now 5
var is now 5

The reason is that the code is evaluated at runtime. When the first setTimeout function fires, the referenced variable i is now set to 5; the same is true for the subsequent firings of setTimeout functions.

The solution is to use JavaScript Closures

The solution to using setTimeout with a JavaScript for loop:

Javascript
var time = 0;
for (var i = 0; i < 5; i++) {
    time += 1000;
    setTimeout(function(j) {
        return function() {
            console.log("var is now", j);
        }
    }(i), time);
}

jQuery select option using text value $.text()

To select an option by searching for the option's text value, use the :contains selector use the filter method.
Javascript
// Select the option using the option's exact text value
var selectOptionText = "Baz";
$("#select-1").find("option").filter(function(index) {
return selectOptionText === $(this).text();
}).attr("selected", "selected");

As of jQuery 1.6, use $.prop() instead of $.attr().
Javascript
var selectOptionText = "wobble";
$("#select-2").find("option").filter(function(index) {
return selectOptionText === $(this).text();
}).prop("selected", "selected");

HTML
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<title></title>
</head>
<body>
 
<!-- Example 1 -->
<select id="select-1">
<option>Select an option...</option>
<option value="1">Foo</option>
<option value="2">Bar</option>
<option value="3">Baz</option>
</select>
 
<script>
// Select the option using the option's exact text value
var selectOptionText = "Baz";
$("#select-1").find("option").filter(function(index) {
return selectOptionText === $(this).text();
}).attr("selected", "selected");
</script>
<!-- /Example 1 -->
 
<!-- Example 2 -->
<select id="select-2">
<option>Pick a different option...</option>
<option value="5">Wibble</option>
<option value="6">wobble</option>
<option value="7">wubble</option>
<option value="8">flob</option>
</select>
 
<script>
// Select the option using the option's exact text value
var selectOptionText = "wobble";
$("#select-2").find("option").filter(function(index) {
return selectOptionText === $(this).text();
}).prop("selected", "selected");
// NOTE: As of jQuery 1.6, use the $.prop() method to retrieve property values because $.attr() only retrieves attributes.
</script>
<!-- /Example 2 -->
 
</body>
</html>

NOTE: Examples using $.find("option:contains('keyword')") have been redacted.

Minimum HTML5 markup for a page that validates successfully

The following is valid HTML5 markup and will validate.
XML
 
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
</body>
</html>

Success! "This document was successfully checked as HTML5!"
Note that you will may get the "experimental feature" warning.
See the Markup Validation Service:
http://validator.w3.org/

Chrome disable referer headers

UPDATE:
Use Google Chrome and Chromium "Preferences" file to permananently disable referrer headers. http://darklaunch.com/2011/08/24/disable-a-referring-url-in-chrome-and-chromium

---

Google Chrome and Chromium send referring headers when you click on a link, submit a form, etc. To disable this "feature", run Chrome or Chromium with the "no-referrers" option.

Code
chromium-browser --no-referrers
 

Allowing Referer Headers

Using the default settings of including referring information may present a privacy/security risk. Requests to web pages may include this information as part of the HTTP Protocol and it tells what page you came from -- this additional information may also be used to track you.

Available Chromium Switches

The full list of available chromium switches is listed here under: "Don't send HTTP-Referer headers"

http://src.chromium.org/svn/trunk/src/content/common/content_switches.cc

Firefox send referer header setting

In Firefox, the option is available in about:config under "network.http.sendRefererHeader" and you want to set this to 0.

  1. 0 - Never send the Referer header or set document.referrer.
  2. 1 - Send the Referer header when clicking on a link, and set document.referrer for the following page.
  3. 2 - Send the Referer header when clicking on a link or loading an image, and set document.referrer for the following page. (Default)

JavaScript document.referrer field

The referring url is available from the document.referrer field.

svn add recursively (the equivalent of "git add .")

One solution to add files recursively with svn is to use the --force option. An example of this is:
Code
 
svn add * --force
 

There is another solution. This is what I came up with:
Code
 
php -r '$files = array(); foreach (simplexml_load_string($argv["1"])->target->entry as $entry) { if ($entry->{"wc-status"}->attributes()->item == "unversioned") { $files[] = chr(34) . (string)$entry->attributes()->path . chr(34); }; } echo implode(" ", $files) . "\n"; ' "$(svn status --xml)" | xargs svn add
 

How this Works


1. svn status is passed/piped to php via xml.
2. php reads the xml and looks for anything that is "unversioned" and echos the filename
3. xargs picks up the echoed filenames and executes svn add to each of the files
And as an alias for recursively svn adding unversioned files:
Code
 
# svn add unversioned files
alias sau="php -r '\$files = array(); foreach (simplexml_load_string(\$argv[\"1\"])->target->entry as \$entry) { if (\$entry->{\"wc-status\"}->attributes()->item == \"unversioned\") { \$files[] = chr(34) . (string)\$entry->attributes()->path . chr(34); }; } echo implode(\" \", \$files) . \"\n\"; ' \"\$(svn status --xml)\" | xargs svn add"