1024×768 Bookmarklet
The days of 800x600 screens are all but over, but most of us still try to accommodate 1024px wide screens. Hence the popularity of "960" width sites. This bookmarklet will resize the current browser window to that width and height. You know, so us web designers with giant monitors can see what it's like to be slummin' with a 1024 screen. Also, to see "the fold", if such a concept even matters anymore.
javascript:resizeTo(1024,768)


Auto Select Textarea Text
<textarea rows="10" cols="50" onclick="this.focus();this.select()" readonly="readonly">
example text
</textarea>



Basic Alert
<script type="text/javascript">
       alert('ALERT!')
</script>



Call Function with Random Timer
function randRange(data) {
var newTime = data[Math.floor(data.length * Math.random())];
return newTime;
}
function toggleSomething() {
var timeArray = new Array(200, 300, 150, 250, 2000, 3000, 1000, 1500);
// do stuff, happens to use jQuery here (nothing else does)
$("#box").toggleClass("visible");
clearInterval(timer);
timer = setInterval(toggleSomething, randRange(timeArray));
}
var timer = setInterval(toggleSomething, 1000);
// 1000 = Initial timer when the page is first loaded



Clear Field on Focus
<input type="text" onfocus="if(this.value == 'value') { this.value = ''; }" value="value" />

Replace "value" with the default value. If the field is selected, the default value will go away. If the user has previously changed the field value, it'll be left alone.
Alternatively, use onfocus="this.value='';" to always clear the field.



Current Page with JavaScript
This is like a replacement for PHP's SCRIPT_NAME with JavaScript.

location.href.split('/').pop();
For example with this URL:
http://css-tricks.com/examples/ScriptName/index.php

This code:
document.write( location.href.split('/').pop() );
Would write to the page: "index.php"




Detect Internet Explorer
<script type="text/javascript">
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 if (ieversion>=8)
  document.write("You're using IE8 or above")
 else if (ieversion>=7)
  document.write("You're using IE7.x")
 else if (ieversion>=6)
  document.write("You're using IE6.x")
 else if (ieversion>=5)
  document.write("You're using IE5.x")
}
else
 document.write("n/a")
</script>



Different Stylesheets for Different Days of the Week
<script type="text/javascript">
<!--
dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
dayNumber = new Date().getDay();
document.writeln('<link rel="stylesheet" type="text/css" href="' + dayNames[dayNumber] + '.css">');
//-->
</script>

<noscript>
<link rel="stylesheet" type="text/css" href="default.css">
</noscript>



Empty an Array

This is one of the fastest and easiest ways of emptying an array. Of course there are may other ways,
but those usually include creation of a new array. This way you reuse the same array.

var myArray = ["one", "two", "three"];

// console.log( myArray ) => ["one", "two", "three"]

myArray.length = 0;

// console.log( myArray ) => []

CSS Menu Samples