Dark Launch

This is a Dark Launch.

jQuery open external links in new window

To open external links in new window, use the following jQuery JavaScript:
Javascript
 
$("a[href^='http:']").not("[href*='" + window.location.hostname + "']").attr('target','_blank');

PHP Encode JavaScript String; encode double quotes, single quotes; onclick parameter

PHP
 
function js_escape($str) {
for ($i = 0, $l = strlen($str), $new_str = ''; $i < $l; $i++) {
$new_str .= (ord(substr($str, $i, 1)) < 16 ? '\\x0' : '\\x') . dechex(ord(substr($str, $i, 1)));
}
return $new_str;
}

Example:
PHP
 
$var = '"Convinced myself, I seek not to convince." -Edgar Allan Poe';

Javascript
 
function doSomething(foo) {
alert(foo);
}
 

XML
 
<input value="Click me" onclick="doSomething('<?php echo js_escape($var); ?>');" type="button" />
<!--
<input value="Click me" onclick="doSomething('\x22\x43\x6f\x6e\x76\x69\x6e\x63\x65\x64\x20\x6d\x79\x73\x65\x6c\x66\x2c\x20\x49\x20\x73\x65\x65\x6b\x20\x6e\x6f\x74\x20\x74\x6f\x20\x63\x6f\x6e\x76\x69\x6e\x63\x65\x2e\x22\x20\x2d\x45\x64\x67\x61\x72\x20\x41\x6c\x6c\x61\x6e\x20\x50\x6f\x65');" type="button" />
-->

JavaScript Timer; JavaScript Duration; Measure the duration of a JavaScript event

To time and measure the duration of a JavaScript event.
Javascript
 
function start() {
startTime = new Date().getTime();
}
 
function stop() {
var endTime = new Date().getTime();
return endTime - startTime;
}

Example:
Javascript
 
function start() {
startTime = new Date().getTime();
}
 
function stop() {
var endTime = new Date().getTime();
return endTime - startTime;
}
 
function doSomething() {
for (var i = 0; i < 1000000; i++) {
 
}
}
 

XML
 
<input value="Click me" onclick="start(); doSomething(); alert(stop() + 'ms');" type="button" />

Outlook View Email Message Source; Outlook 2007

To view the message source of an email, open the email in it's own window. On the Ribbon at the top, select the "Message" tab, then click the "Other Actions" button and select "View Source".

PHP Empty Explode

Explode a string with no empty elements. Similar to C# Split String: StringSplitOptions.RemoveEmptyEntries.
PHP
 
function eexplode($separator, $string) {
$array = explode($separator, $string);
foreach ($array as $key => $val) {
if (empty($val)) {
unset($array[$key]);
}
}
return $array;
}

NOTE: preg_split() using the PREG_SPLIT_NO_EMPTY flag is another option.

PHP Escape String

PHP
 
function escape($str) {
return str_replace(
array(
'&',
'<',
'>',
'"',
'\'',
),
array(
'&amp;',
'&lt;',
'&gt;',
'&quot;',
'&#039;',
),
$str
);
}

NOTE: &apos; is not used as it is not a valid HTML entity reference.